UIImageView,Image在重建项目时不会加载

时间:2016-05-24 05:28:29

标签: ios objective-c core-data uiimageview uiimage

所以我将我的图像保存在我的本地目录中..

NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
                NSString * basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
                UIImage * imageToSave = self.imgViewUser.image;
                NSData * binaryImageData = UIImagePNGRepresentation(imageToSave);

                [binaryImageData writeToFile:
                 [basePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png",self.txtUserName.text ]] atomically:YES] ;

                NSString *final = [NSString stringWithFormat:@"%@/%@.png",basePath,self.txtUserName.text];

[user setValue:final forKey:@"imagePath"];

我正在使用CoreData来保存它。然后在检索到其他控制器时,我这样做..

NSString *imagePath = [self.user valueForKey:@"imagePath"];
UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
self.imgViewUser.image = image;

首先添加我的图像并搜索用户后,我可以看到图像,但是当我重新运行项目时,图像无法加载。救命! :)

1 个答案:

答案 0 :(得分:1)

我在过去的一个项目中遇到过这个问题。

请勿保存完整的图像路径。

您只需存储图像,每次运行项目时,只需附加" username.png"走向你的道路。这将适用于每一次。