Jonathan Thurft
Jonathan Thurft

Reputation: 4173

Resizing an image from UIImagePickerController not working

I am following this post on how to resize an image. I placed the + (UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize; in selectExerciseImageViewController.h and copied the relevant code to selectExerciseImageViewController.m.

Then I attempt to resize the image, save it, and retrieve the saved image file into a UIImageView. but for some reason the image is never showing the the UIImageView and the last NSLog returns null

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

    [picker dismissModalViewControllerAnimated:YES];

  UIImage* newImage = [selectExerciseImageViewController imageWithImage:[info objectForKey:@"UIImagePickerControllerOriginalImage"]
      scaledToSizeWithSameAspectRatio:CGSizeMake(40.0,40.0)];

    NSData* imageData = UIImagePNGRepresentation(newImage);
    // Give a name to the file
    NSString* imageName = @"MyImage.png";

    // Now, we have to find the documents directory so we can save it
    // Note that you might want to save it elsewhere, like the cache directory,
    // or something similar.
    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString* documentsDirectory = [paths objectAtIndex:0];

    // Now we get the full path to the file
    NSString* fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imageName];

    // and then we write it out
    [imageData writeToFile:fullPathToFile atomically:NO];
    //imageView.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    _testimg.image = [UIImage imageNamed:@"MyImage.png"];
    NSLog(@"asd %@",[UIImage imageNamed:@"MyImage.png"]);

}

Upvotes: 1

Views: 499

Answers (1)

Jim
Jim

Reputation: 73966

[UIImage imageNamed:@"MyImage.png"];

This loads an image from the main application bundle. You are writing the file to the documents directory. You need to instantiate the UIImage object with a different method. Try:

[UIImage imageWithContentsOfFile:fullPathToFile];

Upvotes: 2

Related Questions