Reputation: 15
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
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