-1

我试图在我的应用程序中保存和加载从相机胶卷中挑选的图像。当我按下加载或保存按钮时出现错误

*由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[UIImageView CGImage]:无法识别的选择器发送到实例 0xe637640”

有什么想法吗?

AppDelagate.m

- (NSURL *)applicationDocumentsDirectory
{
    return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory i       nDomains:NSUserDomainMask] lastObject];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

我试图保存/加载图像的类的.m

- (IBAction)save:(id)sender {
UIImage *myImage = imageView;
NSData *data = UIImagePNGRepresentation(myImage);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *appDocsDirectory = [paths objectAtIndex:0];


}

- (IBAction)load:(id)sender {
UIImage *myImage = imageView;
NSData *data = UIImagePNGRepresentation(myImage);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *appDocsDirectory = [paths objectAtIndex:0];
UIImage* thumImage = [UIImage imageWithContentsOfFile: [NSString stringWithFormat:@"%@/%@.png", appDocsDirectory, @"myNewFile"]];
}
4

1 回答 1

1

这个错误意味着消息 CGImage 被发送到 UIImageView 类型的对象,但 UIImageView 没有响应它。问题似乎出在

UIImage *myImage = imageView;

您没有正确编写 imageView 的实现,所以我不能准确地说,但我假设 imageView 是 UIImageView,它是 UIView 的子类,并且您将它分配给 UIImage。尝试

UIImage *myImage = [imageView image];

反而。我认为这应该有效))

顺便说一句,为什么你需要-load 中的第一串和第二串代码?它们是不必要的:

- (IBAction)load:(id)sender {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *appDocsDirectory = [paths objectAtIndex:0];
UIImage* thumImage = [UIImage imageWithContentsOfFile: [NSString stringWithFormat:@"%@/%@.png", appDocsDirectory, @"myNewFile"]];
}
于 2012-05-04T22:37:56.620 回答