【问题标题】:Resizing an image in uiimage and saving it with the new size to the photo album调整uiimage中的图像大小并将其以新大小保存到相册
【发布时间】: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

【问题讨论】:

    标签: objective-c resize uiimage save


    【解决方案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 替换为您要绘制的调整大小的图像视图。

    【讨论】:

    • 我明白了:: CGContextDrawImage: invalid context 0x0 当我使用这个时: UIImageView* selectedimage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"whatever"]]; CGSize size = selectedimage.bounds.size; UIGraphicsBeginImageContext(大小); CGContextRef 上下文 = UIGraphicsGetCurrentContext(); CGContextDrawImage(context, selectedimage.bounds, selectedimage.image.CGImage); UIImage *resized = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); UIImageWriteToSavedPhotosAlbum(resized, self, nil, nil);
    • 我现在已经保存了图像,但它最终倒置了?
    • 啊,我一直忘记发生这种情况。我编辑了我的原始答案以添加上下文转换。如果您想知道为什么会发生这种情况,我很确定这是因为 Cocoa 绘图原点位于左上角(就像字面上的任何其他绘图原点一样),但 Core Graphics 的原点位于左下角。
    • 谢谢老兄!!完美运行。在 UIImageView* image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"whatever"]]; 前面仍然显示一个小的黄色警告。当我将图像更改为 imageView 并将其余图像更改为 _imageView 时。这虽然有效,但很烦人。
    • 您好,我还有一个问题想请您帮忙?与上面的代码相同,但我想知道是否可以更改它,以便将图像的新大小存储到 uiimageview 以便通过点击另一个按钮直接邮寄而不保存到相册?
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2013-07-26
      • 2011-10-24
      • 1970-01-01
      • 2021-04-10
      • 2011-01-25
      • 1970-01-01
      • 1970-01-01
      • 2013-08-20
      • 1970-01-01
      相关资源
      最近更新 更多