我尝试在图片视图中加载相册中的图片,然后点击按钮调整大小,然后按另一个按钮以使用新尺寸保存图片。
Evrything运行良好,但保存的图像与原始图像大小相同。
这是我到目前为止所做的:
- (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
答案 0 :(得分: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);
将image
替换为您要绘制的已调整大小的图像视图。
答案 1 :(得分:0)
您可以通过创建UIImage类别来重用此调整大小代码,还有一个现有的良好UIImage类别可以进行图像调整大小,很少有其他东西可以找到它的用法here。