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 trialBowen Smith
15,223 PointsCan't Send Image or Video in Ribbit app, Get alertView "Try Again!"
I've just finished the video 'Uploading the File and Message' in the 'Capturing Photo and Video Using UIImagePickerController' stage of the self-destructing message app.
I tested it on my iPhone 5 by trying to send both a picture and video. Both times when I select my recipients and hit 'Send' it gives me the alertView message "Try again!", as if no matter what the image and video both come up as nil in the if statement.
if (self.image == nil && [self.videoFilePath length] == 0)
At first I thought that placing the
[self reset];
in the wrong spot but moving it around didn't seem to help.
Here's the full section of code:
- (IBAction)send:(id)sender {
if (self.image == nil && [self.videoFilePath length] == 0) {
UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:@"Try again!" message:@"Please select a photo or video to share." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertview show];
[self presentViewController:self.imagePicker animated:NO completion:nil];
}
else {
[self uploadMessage];
[self.tabBarController setSelectedIndex:0];
}
}
#pragma mark - Helper methods
-(void)uploadMessage {
NSData *fileData;
NSString *fileName;
NSString *fileType;
if (self.image != nil) {
UIImage *newImage = [self resizeImage:self.image toWidth:320.0f andHeight:480.0f];
fileData = UIImagePNGRepresentation(newImage);
fileName = @"image.png";
fileType = @"image";
}
else {
fileData = [NSData dataWithContentsOfFile:self.videoFilePath];
fileName = @"video.mov";
fileType = @"video";
}
PFFile *file = [PFFile fileWithName:fileName data:fileData];
[file saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (error) {
UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:@"An Error Occurred!" message:@"Please try sending your message again." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertview show];
}
else {
PFObject *message = [PFObject objectWithClassName:@"Messages"];
[message setObject:file forKey:@"file"];
[message setObject:fileType forKey:@"fileType"];
[message setObject:self.recipients forKey:@"recipientIds"];
[message setObject:[[PFUser currentUser] objectId] forKey:@"senderId"];
[message setObject:[[PFUser currentUser] username] forKey:@"senderName"];
[message saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (error) {
UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:@"An Error Occurred!" message:@"Please try sending your message again." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertview show];
}
else {
//Everything was successful!
[self reset];
}
}];
}
}];
}
- (void)reset {
self.image = nil;
self.videoFilePath = nil;
[self.recipients removeAllObjects];
}
Has anyone else ran into something like this?
PS I'm using Xcode 5 and testing on iOS 7.1 on an iPhone 5.
1 Answer
landonferrier
25,097 PointsBowen, here is a full template for the social network application: Click Here.
Bowen Smith
15,223 PointsBowen Smith
15,223 PointsMy bad, I found the problem by tracing it backwards.
It was just an autofill error. When saving the media I had defined "imagePickerControllerDidCancel" twice, instead of defining that the first time and just "imagePickerController" the second time.
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
Just one of those things...focus, focus, focus.