Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialKieran Robinson
9,411 PointsSaving Images - The Diary App
I have recently finished the Core Data Diary app, and i have found a bug within the project. Once the user has selected an image for the diary post and pressed 'done', the image is saved. However if the user then wants to change the text within the post by clicking on the cell, the image is removed upon pressing done. if anyone has any ideas on the fix please let me know!
12 Answers
David Collins
6,643 PointsI solved this a different way by adding
[self setPickedImage:[UIImage imageWithData:self.entry.imageData]];
into the if statement in viewDIdLoad where the rest of the data is being retrieved in if self.entry != nil (where you're loading an existing record)
I think by doing it the other way you're just repeating the setPickedImage method.
Thomas Nilsen
14,957 PointsOk got it! The first thing I wrote was (almost) correct but here is everything you have to do (everything in THEntryViewController)
at the bottom of viewDidLoad:
if (self.entry.imageData != nil) {
UIImage *image = [UIImage imageWithData:self.entry.imageData];
[self.imageButton setImage:image forState:UIControlStateNormal];
}
And, in the - (void) updateDiary method, do this when updating the image
if (self.pickedImage != nil) {
self.entry.imageData = UIImageJPEGRepresentation(self.pickedImage, 0.75);
} else {
self.entry.imageData = UIImageJPEGRepresentation(self.imageButton.imageView.image, 0.75);
}
Hope this helps :)
Paul Cisneros
Courses Plus Student 2,117 PointsThis works! But I'm curious why you used "imageView"? Was that property declared somewhere? Anyway, thanks!
Kieran Robinson
9,411 PointsPaul Cisneros, check out the best answer on the post, it fixes the issue and does it in one line!
Thomas Nilsen
14,957 PointsGo in THEntryViewController and add the following at the bottom of viewDidLoad:
UIImage *image = [UIImage imageWithData:self.entry.imageData];
[self.imageButton setImage:image forState:UIControlStateNormal];
Maxim Kontsevitch
25,447 PointsI had that problem too
Kieran Robinson
9,411 PointsIm not sure why it happens either, we have set it up the same as the labels etc.. Any help Ash Furrow??
Kieran Robinson
9,411 PointsHi Thomas! Thank you for the reply, the code works first time, in that i mean it shows the image when i press on the cell to edit again, its there in its little circle. however when i press done the image disappears again, along with the 'no image' image in the edit screen!
Thomas Nilsen
14,957 PointsI just realized that after I posted this. I'll have a further look :)
Kieran Robinson
9,411 PointsThank you so much Thomas! It worked perfectly!
Lionel Martin
3,823 PointsHi All, and many thanks to Thomas for fixing the issue. To keep the image when editing the Entry and pressing done, follow the instruction of Thomas, but in - (void) updateDiary method, simply delete the else condition. It seems to work for me
Lionel Martin
3,823 PointsIt seems that when editing a post and not changing the image you don't call any method and so the self.pickedImage is nil, then you erase the entry.imageData using your else condition.
Kieran Robinson
9,411 PointsHi Lionel Martin, i agree with removing the else condition, it worked too!
Kieran Robinson
9,411 PointsDid your way work David Collins? I will have to try it now. Also, how would you solve the issue in that when i go to edit an entry, the button's image at the top left of the screen (the one that we press to prompt for camera / photo roll), does not retain the image i previously selected?
Kieran Robinson
9,411 PointsThank you David Collins, the solution presented fixed both the initial problem, and the one i mentioned in my last comment!
David Collins
6,643 PointsYou're welcome, Glad I could help :-)
Nick Ross
20,314 PointsI used this solution (http://stackoverflow.com/a/22369085) to make selecting the image more user friendly.
In the EntryView Controller:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *image = info[UIImagePickerControllerOriginalImage];
self.pickedImage = image;
[picker dismissViewControllerAnimated:YES completion:nil]; //Added this line
}
I did this in conjunction with the method David described above and it works fine on my app.
Jonathan Fernandez
8,325 PointsJonathan Fernandez
8,325 PointsThis one line is not yet complete... If you click on an existing entry with no image added.. In the next view shown, the no image icon template that is suppose to be set by default is missing from the view.
I have been able to resolve this by adding a check inside the if self.entry != nil area.
Shown below is my code:
David Collins
6,643 PointsDavid Collins
6,643 PointsHi Jonathan, I don't seem to be having bug with the no icon image.
What's your setPickedImage method looking like? This is where the it checks for the nil image.
-(void)setPickedImage:(UIImage *)pickedImage {
}
Let me know if that helps.
Jonathan Fernandez
8,325 PointsJonathan Fernandez
8,325 PointsAh I see.. yes it makes sense now cause I had my setPickedImage like this:
Thanks for pointing it out. When I run it now with the else statement added I no longer need to perform the check in the viewDidLoad. : )