Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Build a Self-Destructing Message iPhone App Retrieving and Viewing Data from Parse.com Viewing Images Using UIImageView

Error when showing image

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    PFFile *imageFile = [self.message objectForKey:@"file"];

    NSURL *imageFileURL = [[NSURL alloc] initWithString:imageFile.url]; // i get an error here
    NSData *imagedata = [NSData dataWithContentsOfURL:imageFileURL];
    self.imageView.image = [UIImage imageWithData:imagedata];
}

In console it says : Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSURL initWithString:relativeToURL:]: nil string parameter'

Checked and everything else in uploadMessage method is correct, and it's also spelled file correctly.

6 Answers

Patrick Donahue
Patrick Donahue
9,523 Points

Sorry I am just trying to follow the code along. Next would be the didSelectRowAtIndexPath in the InboxViewController. This is what I have:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.selectedMessage = [self.messages objectAtIndex:indexPath.row];
    NSString *fileType = [self.selectedMessage objectForKey:@"fileType"];
    if ([fileType isEqualToString:@"image"]) {
        [self performSegueWithIdentifier:@"showImage" sender:self];
    } else {
        PFFile *videoFile = [self.selectedMessage objectForKey:@"file"];
        NSURL *fileURL = [NSURL URLWithString:videoFile.url];
        self.moviePlayer.contentURL = fileURL;
        [self.moviePlayer prepareToPlay];
//        [self.moviePlayer thumbnailImageAtTime:0 timeOption:MPMovieTimeOptionNearestKeyFrame];

        [self.view addSubview:self.moviePlayer.view];
        [self.moviePlayer setFullscreen:YES animated:YES];

    }

    NSMutableArray *recipientIds = [NSMutableArray arrayWithArray:[self.selectedMessage objectForKey:@"recipientIds"]];
    if ([recipientIds count] == 1) {
        [self.selectedMessage deleteInBackground];

    } else {
        [recipientIds removeObject: [[PFUser currentUser] objectId]];
        [self.selectedMessage setObject:recipientIds forKey:@"recipientIds"];
        [self.selectedMessage saveInBackground];
    }
}

The only differences we have are that you have "recipientIDs" and I have "recipientsIds". And that doesn't matter if you are seeing everything.

Patrick Donahue
Patrick Donahue
9,523 Points

I have a feeling you may have a "file" key or a "fileType" key somewhere that is wrong.

Patrick Donahue
Patrick Donahue
9,523 Points

Can you see the image in the Parse.com dashboard?

Yes, so do the recipients, it also shows in NSLog

Patrick Donahue
Patrick Donahue
9,523 Points

HMM.... try this Stack Overflow answer.

Your code looks the same as mine. But I did this a while ago and not sure how it flows from a quick view. I will look more into the project.

Patrick Donahue
Patrick Donahue
9,523 Points

Double check the uploadMessage method in the CameraViewController. Something might be uploading incorrectly.

ok, the urlWithString method worked, it doesn't crash, but now the app itself is messed up, the image it self doesn't show, and when i try to send an image again, recipients don't show, although they used to show before.

Patrick Donahue
Patrick Donahue
9,523 Points

Switch it back to initWithString and lets look at the upLoadMEssage method.

Switched back and same error occurred.

-(void) uploadMessage {

    NSData *fileData;
    NSString *fileName;
    NSString *fileType;

   //check if it's image or video
    if (self.image != nil) {
        UIImage *newImage = [self resizeImage:self.image toWidth:320.0f andHight: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:@"Error Occured" message:@"Please try sending again" delegate:self cancelButtonTitle:@"Got It" otherButtonTitles:nil, 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:@"Error Occured" message:@"Please try sending again" delegate:self cancelButtonTitle:@"Got It" otherButtonTitles:nil];
                    [alertView show];
                }
                else {
                    //everything was successful, nothing to be done here because the user will be sent back to inbox in the original method
                    [self reset];

                }
            }];


        }
    }];

   //if image, shrink it
   //Upload the file itself
   //Upload message details

}

Problem Solved. It was the prepareForSegue in inboxViewController. My God apps are very boring, i got into programming in order to make games, they are my passion and i have so many great ideas for making them. I think i will jump into game developing.

Anyway Thank You patrick for your efforts. :)

Patrick Donahue
Patrick Donahue
9,523 Points

You're welcome!

Unity and Unreal are good game editors. But Unreal isn't for Mac. :-(