Reputation:
For a profile page, I let users choose an image if they want. Otherwise, the page displays a generic headshot. In the edit version of the page, it similarly shows the image they pick using the image picker controller or a generic headshot if they have never chosen one.
When I save in core data, if they have chosen image, I save the image as a file and save its name in core data. That is working.
However, if they have not chosen an image and the generic headshot is showing, I don't want to save anything--either an image or a value for pic name in core data.
Right now, I am grabbing the image name and contents from the view and discovered I am inadvertently saving the generic image view and also the generic image name in core data which causes problems down the line.
How can I grab the image and name if is a chosen image but if only a generic one, detect this so that I don't save anything?
This is abbreviated code I am currently using:
//grab image currently being displayed
UIImage *image = self.imageView.image;
//assign name for pic
NSString *picname = @"newpic.png";
//***I would like to do following only if the user has chosen a pic, not if it is a generic pic showing.
//save name of pic in contact entity
[self.contact setValue:picname forKey:@"pic"];
//use separate saveImageasPicName method to save the image
[self saveImage:image asPicName:picname];
//****
//save managed object context which puts name of pic in core data
if ([self.managedObjectContext save:&error]) {
//saved
}
Upvotes: 0
Views: 127
Reputation: 2413
You can declare property
@property (nonatomic, assign) BOOL imageWasPicked;
on your viewDidload set it to NO
self.imageWasPicked = NO;
After you picked the image, set flag to YES
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
// image processing code
self.imageWasPicked = YES;
}
and, finally, check this flag - save data or not
if (self.wasImagePicked)
{
//grab image currently being displayed
UIImage *image = self.imageView.image;
//assign name for pic
NSString *picname = @"newpic.png";
//***I would like to do following only if the user has chosen a pic, not if it is a generic pic showing.
//save name of pic in contact entity
[self.contact setValue:picname forKey:@"pic"];
//use separate saveImageasPicName method to save the image
[self saveImage:image asPicName:picname];
//****
//save managed object context which puts name of pic in core data
if ([self.managedObjectContext save:&error]) {
//saved
}
}
Hope this helps
Upvotes: 1