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 Build a Self-Destructing Message iPhone App Capturing Photo and Video Using UIImagePickerController Resizing an Image and Getting Ready to Send

Caleb Kleveter
MOD
Caleb Kleveter
Treehouse Moderator 37,862 Points

Confused.

I'm a little confused about this challenge, can anybody expand on these instructions?

"In this Latergram app, we only want to upload an image if the 'image' and 'caption' properties of the view controller below are set. In the 'upload' method, add a check to make sure that both properties have values before executing the block of code. Note: 'image' is a UIImage and 'caption' is an NSString."

LatergramViewController.m
#import "LatergramViewController.h"

@implementation LatergramViewController

- (IBAction)upload {

    CGSize newSize = CGSizeMake(320.0f, 480.0f);
    CGRect newRectangle = CGRectMake(0, 0, 320.0f, 480.0f);
    UIGraphicsBeginImageContext(newSize);

    // Task 2: Line missing here! How do we draw the image in the resized context?

    UIImage *resizedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    // Rest of code omitted for brevity!
}

@end
LatergramViewController.h
#import "UIViewController.h"
#import "UIImage.h"

@interface LatergramViewController : UIViewController

@property (nonatomic, strong) UIImage *image;
@property (nonatomic, assign) NSString *caption;

- (IBAction)upload;

@end
Jayden Spring
Jayden Spring
8,625 Points

Hi Caleb,

I may be wrong but ill give it a shot! Have a look at this; https://developer.apple.com/library/ios/samplecode/sc2273/Listings/AirDropSample_UIImage_Resize_m.html

By the looks of it they want you to check that there is an image when the button is clicked, as im guessing it could be clicked before the photo picker is shown. They want you to resize the image within the context of the new size. Then just check if the caption is present at the end.

Hope this is of some help without looking at the challenge!

1 Answer

Zachary Betz
Zachary Betz
10,413 Points

To check if the image and caption properties of the view controller are set, add an IF statement that checks if the values of the properties are nil. Here are two ways to go about this:

1) Think of nil as nothing.

if (image != nil && caption != nil)
{
  //add your code here
}
else
{
  NSLog(@"The properties are not set."); 
}

2) Here, you're doing the same thing, but in reverse: instead of checking to see if the properties are not nothing, you are seeing if they are something, i.e. do they have a value?

if (image && caption)
{
  //add your code here
}
else
{
  NSLog(@"The properties are not set."); 
}
Caleb Kleveter
Caleb Kleveter
Treehouse Moderator 37,862 Points

Wait, I thought that != is the same as not equal, in that case "image != nil" is the same as "image" is equal to something, or is not nil, couldn't I just write "image"?

Zachary Betz
Zachary Betz
10,413 Points

Caleb Kleveter, you are right! I just wanted to show you two ways of doing the same thing.

Jayden Spring
Jayden Spring
8,625 Points

If image has a value then it wouldn't be nil. Zachary is correct, image would be true (ie not nill) if there was a value.

Personally I would go with image != nil && caption != nil as it is a bit easier to understand if you decide to go back in a few days after banging your head at the keyboard!


Ignore the above, im jumping in and out of obj-c and swift and confusing myself and for some reason decided to think Zachary's not equal to was equal to!

Zachary Betz
Zachary Betz
10,413 Points

Caleb Kleveter, if my reply answered your question, feel free to mark it as best. That way, other students will be able to learn from your post. If not, please let me know!

Caleb Kleveter
Caleb Kleveter
Treehouse Moderator 37,862 Points

Thanks Zachary for your help, you got me on the right track (I did re-watch the video though :) ).

Zachary Betz
Zachary Betz
10,413 Points

Glad I could help, Caleb Kleveter! Keep up the hard work :)