0

I'm trying to load an image from the photo album in an image view and then hit a button to resize and then another to save the image with the new size.

Evrything is working well except that the image saved has the same size as the original.

this is what I did so far:

    - (IBAction)chooseImage:(id)sender
{
    self.imagePicker = [[UIImagePickerController alloc] init];
    self.imagePicker.delegate = self;
    [self.imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
    [self presentViewController:self.imagePicker animated:YES completion:nil];
    }

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    self.chosenImage = info[UIImagePickerControllerOriginalImage];
    [self.imageView setImage:self.chosenImage];
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void) imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [self dismissViewControllerAnimated:YES completion:nil];

}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)reSize:(id)sender

{
    CGRect newFrame = CGRectMake(20, 49, 280, 200);
    [UIView animateWithDuration:0.25f
                     animations:^{
                         self.imageView.frame = newFrame;
                     }];
}

- (IBAction)saveImage:(id)sender
{
       UIImageWriteToSavedPhotosAlbum(_chosenImage, self, nil, nil);
}


@end
Tim
  • 8,932
  • 4
  • 43
  • 64
John Jay
  • 15
  • 6

2 Answers2

3
UIImageView* image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"whatever"]];

CGSize size = image.bounds.size;
UIGraphicsBeginImageContext(size);

CGContextRef context = UIGraphicsGetCurrentContext();
CGAffineTransform flipVertical = CGAffineTransformMake(
        1, 0, 0, -1, 0, size.height
);
CGContextConcatCTM(context, flipVertical);  
CGContextDrawImage(context, image.bounds, image.image.CGImage);
UIImage *resized = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

UIImageWriteToSavedPhotosAlbum(resized, self, nil, nil);

Replace image with the resized imageview that you want to draw.

Michael Celey
  • 12,645
  • 6
  • 57
  • 62
Heckman
  • 398
  • 1
  • 13
  • I get this: : CGContextDrawImage: invalid context 0x0 When i use this: UIImageView* chosenimage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"whatever"]]; CGSize size = chosenimage.bounds.size; UIGraphicsBeginImageContext(size); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextDrawImage(context, chosenimage.bounds, chosenimage.image.CGImage); UIImage *resized = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); UIImageWriteToSavedPhotosAlbum(resized, self, nil, nil); – John Jay Apr 03 '13 at 01:55
  • i got so far as to save the image now, but it ends up upside down? – John Jay Apr 03 '13 at 04:18
  • Ah I keep forgetting that happens. I Edited my original answer to add the Context Transform. If you're wondering why this happens, I'm pretty sure it's because the Cocoa drawing origin is in the top left (like literally any other drawing origin in anything else) but the origin for Core Graphics is the bottom left. – Heckman Apr 03 '13 at 14:09
  • Thanks mate!! works perfectly. There's still one minor yellow warning that displays infront of UIImageView* image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"whatever"]]; when i change image to imageView and the rest to _imageView. This is working though, but annoying. – John Jay Apr 03 '13 at 14:21
  • Hello again, I have another question that you maybe can help me with? It's the same code as above, but I wonder if it could be altered so that the new size of the image would be stored to the uiimageview in order to mail it directly by hitting another button without saving to photo album? – John Jay Apr 04 '13 at 15:56
  • Yep, I did that exact thing not too long ago. Create a new MFMailComposeViewController (you may have to add the MessageUI framework). Then use the message "addAttachment" and pass either the UIImagePNGRepresentation or UIImageJPEGRepresentation to the data value for it. This SO question should help you http://stackoverflow.com/questions/1389655/iphone-email-attachment – Heckman Apr 08 '13 at 14:15
0

You can reuse this resize code by creating an UIImage category, there's also an existing good UIImage category that can do image resize and few other things an good example of its usage can be found here.

Community
  • 1
  • 1
ararog
  • 1,092
  • 10
  • 18