调整uiimage中的图像大小并将其以新大小保存到相册
Posted
技术标签:
【中文标题】调整uiimage中的图像大小并将其以新大小保存到相册【英文标题】:Resizing an image in uiimage and saving it with the new size to the photo album 【发布时间】:2013-04-02 21:50:20 【问题描述】:我正在尝试在图像视图中加载相册中的图像,然后单击一个按钮来调整大小,然后单击另一个按钮以使用新大小保存图像。
一切都很好,只是保存的图像与原始图像大小相同。
这是我到目前为止所做的:
- (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
【问题讨论】:
【参考方案1】: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);
将image
替换为您要绘制的调整大小的图像视图。
【讨论】:
我明白了:您可以通过创建 UIImage 类别来重用此调整大小代码,还有一个现有的很好的 UIImage 类别可以进行图像调整大小和其他一些事情,一个很好的使用示例可以在 here 找到。
【讨论】:
以上是关于调整uiimage中的图像大小并将其以新大小保存到相册的主要内容,如果未能解决你的问题,请参考以下文章