>我有这个相对简单的辅助方法:
- (float)imageHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
float imageWidth = [[self.widthArray objectAtIndex:indexPath.row] floatValue];
float ratio = screenWidth/imageWidth;
float imageHeight = ratio * [[self.heightArray objectAtIndex:indexPath.row] floatValue];
return imageHeight;
}
在另一个方法中调用时,它在此方法中完全正常工作:
- (UIImage *)imageWithImage:(UIImage *)image forRowAtIndexPath:(NSIndexPath *)indexPath
{
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat newWidth = screenRect.size.width;
CGFloat newHeight = [self imageHeightForRowAtIndexPath:indexPath.row];
UIGraphicsBeginImageContext(CGSizeMake(newWidth, newHeight ));
[image drawInRect:CGRectMake(0, 0, newWidth, newHeight)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
编译器说: Implicit conversion from 'NSInteger' to 'NSIndexPath' is disallowed
我不知道为什么以及如何解决这个问题。知道吗?
您有 2 种方法:
-
- (float)imageHeightForRowAtIndexPath:(NSIndexPath *)indexPath
-
- (UIImage *)imageWithImage:(UIImage *)image forRowAtIndexPath:(NSIndexPath *)indexPath
每个参数都有一个NSIndexPath *
参数。
现在,在imageWithImage:forRowAtIndexPath:
中,您正在呼叫imageHeightForRowAtIndexPath:
:
[self imageHeightForRowAtIndexPath:indexPath.row]
在那条线上,您正在从indexPath
(NSInteger
)中获取row
并尝试将其传递给imageHeightForRowAtIndexPath:
:,这期待NSIndexPath *
.
这是导致错误的原因,因为编译器知道期望NSIndexPath *
并且知道您正在提供NSInteger
。
若要解决,请将代码更改为:
CGFloat newHeight = [self imageHeightForRowAtIndexPath:indexPath];