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
codemonkey3
656 PointsUploading the File and Message
Hi, i'm having problem with the last tutorial of "Capturing Photo and Video Using UIImagePickerController". The problem is the image that i upload to the simulator is treated as video. The code runs without error.
Kai Aldag
Courses Plus Student 13,560 Pointsya please put in your code
4 Answers
Amit Bijlani
Treehouse Guest TeacherI'm guessing you resolved this problem.
codemonkey3
656 PointsHi Amit, i still don't know why. If you run this code, the simulator will show "Please capture or select a photo or video to share!" but if you blank out this part:
if (self.image == nil && [self.videoFilePath length] == 0){
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Try again!" message:@"Please capture or select a photo or video to share!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
[self presentViewController:self.imagePicker animated:NO completion:nil];
}
Parse will show the fileType as video when you send the image. Here's the code:
#import "CameraViewController.h"
#import <MobileCoreServices/UTCoreTypes.h>
@interface CameraViewController ()
@end
@implementation CameraViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.friendsRelation = [[PFUser currentUser] objectForKey:@"friendsRelation"];
self.recipients = [[NSMutableArray alloc] init];
}
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
PFQuery *query = [self.friendsRelation query];
[query orderByAscending:@"username"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (error) {
NSLog(@"Error %@ %@", error, [error userInfo]);
}
else {
self.friends = objects;
[self.tableView reloadData];
}
}];
if (self.image == nil && [self.videoFilePath length] == 0){
self.imagePicker = [[UIImagePickerController alloc] init];
self.imagePicker.delegate = self;
self.imagePicker.allowsEditing = NO;
self.imagePicker.videoMaximumDuration = 10;
if([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) {
self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
}
else {
self.imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}
self.imagePicker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:self.imagePicker.sourceType];
[self presentViewController:self.imagePicker animated:NO completion:nil];
}
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section
{
return[self.friends count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
PFUser *user = [self.friends objectAtIndex:indexPath.row];
cell.textLabel.text = user.username;
if ([self.recipients containsObject:user.objectId]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else{
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.tableView deselectRowAtIndexPath:indexPath animated:NO];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
PFUser *user = [self.friends objectAtIndex:indexPath.row];
if (cell.accessoryType == UITableViewCellAccessoryNone){
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[self.recipients addObject:user.objectId];
}
else {
cell.accessoryType = UITableViewCellAccessoryNone;
[self.recipients removeObject:user.objectId];
}
}
#pragma mark - Image Picker Controller delegate
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
[self dismissViewControllerAnimated:NO completion:nil];
[self.tabBarController setSelectedIndex:0];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info{
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
//A photo was taken/selected!
self.image = [info objectForKey:UIImagePickerControllerOriginalImage];
if (self.imagePicker.sourceType == UIImagePickerControllerSourceTypeCamera){
//save the image
UIImageWriteToSavedPhotosAlbum(self.image, nil, nil, nil);
}
}
else {
//A video was taken/selected!
self.videoFilePath = [[info objectForKey:UIImagePickerControllerMediaURL] path];
if (self.imagePicker.sourceType == UIImagePickerControllerSourceTypeCamera){
//save the video
if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(self.videoFilePath)) {
UISaveVideoAtPathToSavedPhotosAlbum(self.videoFilePath, nil, nil, nil);
}
}
}
[self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - IBActions
- (IBAction)cancel:(id)sender {
[self reset];
[self.tabBarController setSelectedIndex:0];
}
- (IBAction)send:(id)sender {
if (self.image == nil && [self.videoFilePath length] == 0){
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Try again!" message:@"Please capture or 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 reset];
[self.tabBarController setSelectedIndex:0];
}
}
#pragma mark - helper methods
- (void)uploadMessage {
//Check if image or video
//If image, shrink it
//Upload the file
//Upload the message
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:fileType 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];
}
- (UIImage *)resizeImage:(UIImage *)image toWidth:(float)width andHeight:(float)height {
CGSize newSize = CGSizeMake(width, height);
CGRect newRectangle = CGRectMake(0, 0, width, height);
UIGraphicsBeginImageContext(newSize);
[self.image drawInRect:newRectangle];
UIImage *resizedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return resizedImage;
}
@end
Scott Bedard
29,196 PointsI found that this line needed to be changed: self.image = [info objectForKey:UIImagePickerControllerOriginalImage];
to:
self.image = [info objectForKey:UIImagePickerControllerReferenceURL];
At least for xcode 5 and to get it to work in the simulator. I am sure it will cause other issues to figure out latter
Scott Bedard
29,196 PointsAh yes, I spoke to soon...when the image goes to get resized it throws an exception now. Need an update to the lesson for ios7?
Amit Bijlani
Treehouse Guest TeacherWhat is the exception?
Scott Bedard
29,196 Pointsdeleted message
Scott Bedard
29,196 PointsWeird, tonight it is working with the original code. Maybe the simulator just needed to be updated? Xcode got updated today as well...so who knows! But I am back in the saddle again. Sorry for the distraction....carry on.
Amit Bijlani
Treehouse Guest TeacherAmit Bijlani
Treehouse Guest TeacherCan you paste your code?