0

我正在尝试在图像视图中从相册加载图像,然后单击一个按钮调整大小,然后单击另一个按钮以新大小保存图像。

除了保存的图像与原始图像大小相同之外,一切都运行良好。

这是我到目前为止所做的:

    - (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
4

2 回答 2

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为您要绘制的调整大小的图像视图。

于 2013-04-02T22:54:25.873 回答
0

您可以通过创建 UIImage 类别来重用此调整大小代码,还有一个现有的很好的 UIImage 类别可以进行图像调整大小和其他一些事情,一个很好的使用示例可以在这里找到。

于 2013-04-06T19:40:21.983 回答