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 trial

iOS

Kieran Robinson
Kieran Robinson
9,411 Points

Saving 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
David Collins
6,643 Points

I 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.

Jonathan Fernandez
Jonathan Fernandez
8,325 Points

This 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:

<p>
    if (self.entry != nil) {
        self.textView.text = self.entry.body;
        self.pickedMood = self.entry.mood;
        date = [NSDate dateWithTimeIntervalSince1970:self.entry.date];
        if (self.entry.imageData != nil) { // Need to add a check for no images otherwise the "noimage" placeholder will be gone.
            [self setPickedImage:[UIImage imageWithData:self.entry.imageData]]; // Line from David Collins (Thanks David!!) : )
        }
    } else {
        self.pickedMood = JWFDiaryEntryMoodGood;
        date = [NSDate date];
        [self loadLocation];
    }
</p>
David Collins
David Collins
6,643 Points

Hi 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 {

_pickedImage = pickedImage;

if (pickedImage == nil) {
    [self.imageButton setImage:[UIImage imageNamed:@"icn_noimage"] forState:UIControlStateNormal];
}   else{
    [self.imageButton setImage:pickedImage forState:UIControlStateNormal];
}

}

Let me know if that helps.

Jonathan Fernandez
Jonathan Fernandez
8,325 Points

Ah I see.. yes it makes sense now cause I had my setPickedImage like this:

<p>
- (void)setPickedImage:(UIImage *)pickedImage {
    _pickedImage = pickedImage;

    if (pickedImage == nil) {
        [self.imageButton setImage:[UIImage imageNamed:@"icn_noimage"] forState:UIControlStateNormal];
    }
    [self.imageButton setImage:pickedImage forState:UIControlStateNormal]; // Woops! Xp

}
</p>

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. : )

Thomas Nilsen
Thomas Nilsen
14,957 Points

Ok 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 :)

This works! But I'm curious why you used "imageView"? Was that property declared somewhere? Anyway, thanks!

Kieran Robinson
Kieran Robinson
9,411 Points

Paul Cisneros, check out the best answer on the post, it fixes the issue and does it in one line!

Thomas Nilsen
Thomas Nilsen
14,957 Points

Go in THEntryViewController and add the following at the bottom of viewDidLoad:

UIImage *image = [UIImage imageWithData:self.entry.imageData];
    [self.imageButton setImage:image forState:UIControlStateNormal];
Kieran Robinson
Kieran Robinson
9,411 Points

Im not sure why it happens either, we have set it up the same as the labels etc.. Any help Ash Furrow??

Kieran Robinson
Kieran Robinson
9,411 Points

Hi 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
Thomas Nilsen
14,957 Points

I just realized that after I posted this. I'll have a further look :)

Kieran Robinson
Kieran Robinson
9,411 Points

Thank you so much Thomas! It worked perfectly!

Hi 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

It 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
Kieran Robinson
9,411 Points

Hi Lionel Martin, i agree with removing the else condition, it worked too!

Kieran Robinson
Kieran Robinson
9,411 Points

Did 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
Kieran Robinson
9,411 Points

Thank you David Collins, the solution presented fixed both the initial problem, and the one i mentioned in my last comment!

David Collins
David Collins
6,643 Points

You're welcome, Glad I could help :-)

Nick Ross
Nick Ross
20,314 Points

I 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.