John Jay
John Jay

Reputation: 15

Resizing an image in uiimage and saving it with the new size to the photo album

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

Upvotes: 0

Views: 688

Answers (2)

Heckman
Heckman

Reputation: 398

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.

Upvotes: 3

ararog
ararog

Reputation: 1092

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.

Upvotes: 0

Related Questions