1

我正在开发相机应用程序。我正在使用 UIImagePickerController 捕获图像。之后,在将其保存到画廊之前,我想裁剪图像并需要将裁剪后的图像保存在画廊中。

我在谷歌上搜索,我找到了从图库中挑选图像然后裁剪的样本,这不是我的要求。任何人都可以解决这个问题吗???如果您浏览过任何教程/示例代码,请发布链接。提前致谢

4

2 回答 2

1

推是允许编辑=是;用于选择器,以便我们也可以编辑您从图库中捕获和选择的图像

UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    picker.delegate = self;
    picker.isallowediting = yes;  

希望这些对你有帮助

于 2013-03-08T06:38:39.293 回答
0

在 imagePickerController 方法中使用此代码来裁剪捕获的图像。它对我来说很好。

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
    {
    [_imagePickerControllerInstance dismissModalViewControllerAnimated:YES];
        UIImage *_pickedAvatar = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

       UIImage *croppedImage = [self cropImage:_pickedAvatar andFrame:CGRectMake(0, 0, 250, 250)];

   // croppedImage : U can use this cropped image for saving in gallery.

    }
- (UIImage *)cropImage:(UIImage*)image andFrame:(CGRect)rect {

   //Note : rec is nothing but the image frame which u want to crop exactly. 

    rect = CGRectMake(rect.origin.x*image.scale,
                      rect.origin.y*image.scale,
                      rect.size.width*image.scale,
                      rect.size.height*image.scale);

    CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], rect);
    UIImage *result = [UIImage imageWithCGImage:imageRef
                                          scale:image.scale
                                    orientation:image.imageOrientation];
    CGImageRelease(imageRef);
    return result;
}

别的

看到这个问题

使用UIImage 类别并在需要裁剪图像的任何地方使用。

@implementation UIImage (Crop)
    - (UIImage *)crop:(CGRect)rect {

        rect = CGRectMake(rect.origin.x*self.scale, 
                          rect.origin.y*self.scale, 
                          rect.size.width*self.scale, 
                          rect.size.height*self.scale);       

        CGImageRef imageRef = CGImageCreateWithImageInRect([self CGImage], rect);
        UIImage *result = [UIImage imageWithCGImage:imageRef 
                                              scale:self.scale 
                                        orientation:self.imageOrientation]; 
        CGImageRelease(imageRef);
        return result;
    }

@end
于 2013-03-08T06:51:49.493 回答