我在这行代码中得到此错误(Void方法'undo:'不应返回值):
return image;
出了什么问题,有人知道这个错误的解决方案吗?
代码:
- (IBAction)undo:(id)sender
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* path = [documentsDirectory stringByAppendingPathComponent:
@"test.png" ];
UIImage* image = [UIImage imageWithContentsOfFile:path];
[self->_imagView setImage:image];
return image;
}
修改
如果点击“撤消”按钮,我应该说什么?图像应该返回?
代码:
- (IBAction)undo:(id)sender
{
}
- (UIImage*)loadImage
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* path = [documentsDirectory stringByAppendingPathComponent:
@"test.png" ];
UIImage* image = [UIImage imageWithContentsOfFile:path];
return image;
}
- (void)saveImage: (UIImage*)image
{
if (image != nil)
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* path = [documentsDirectory stringByAppendingPathComponent:
@"test.png" ];
NSData* data = UIImagePNGRepresentation(image);
[data writeToFile:path atomically:YES];
}
}
答案 0 :(得分:3)
您无法从具有void
返回类型的方法返回值。 IBAction
实际上只是void
。
除了尝试返回之外,您还需要使用image
执行其他操作。
答案 1 :(得分:1)