EdBer EdBer - 6 months ago 8
iOS Question

UIImageView, Image doesn't load when project is rebuilt

So I'm saving my image in my local directory like this..

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"];


Im using CoreData to save it. Then in retrieving it to other controllers, Im doing this..

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


At first after adding my image and search for the user, I can see the image, but when I re run the project, the image doesn't load. help! :)

Answer

I got the problem and I have faced this issue in one of my project in past. Actually what has happened that

don't save your full image path.

You need to store only image and everytime when you run the project you just need to append your "username.png" to your path. This will work for everytime.