尝试在应用程序错误NSInvalidArgumentException中保存/加载图像

时间:2012-05-04 18:48:45

标签: xcode load directory save

我试图在我的应用中保存并加载从相机胶卷中拾取的图像。按下加载或保存按钮时出现错误

* 由于未捕获的异常'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"]];
}

1 个答案:

答案 0 :(得分: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"]];
}