我有这个相对简单的辅助方法:
- (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
我不知道为什么以及如何解决这个问题。有什么想法吗?
答案 0 :(得分:1)
您有两种方法:
- (float)imageHeightForRowAtIndexPath:(NSIndexPath *)indexPath
- (UIImage *)imageWithImage:(UIImage *)image forRowAtIndexPath:(NSIndexPath *)indexPath
每个参数都有一个NSIndexPath *
。
现在,在imageWithImage:forRowAtIndexPath:
您正在呼叫imageHeightForRowAtIndexPath:
:
[self imageHeightForRowAtIndexPath:indexPath.row]
并且在该行上,您正在从row
(indexPath
)获取NSInteger
并尝试将其传递给imageHeightForRowAtIndexPath:
:,这需要NSIndexPath *
NSIndexPath *
1}}。
这是错误的原因,因为编译器知道期望NSInteger
并且它知道您提供了CGFloat newHeight = [self imageHeightForRowAtIndexPath:indexPath];
。
要解决此问题,请将代码更改为:
{{1}}