裁剪图像的图像裁剪问题

时间:2017-01-07 08:03:21

标签: ios objective-c iphone

我想裁剪图片,我搜索了很多图书馆,但没有找到完美答案。

我想使用矩形形状裁剪图像,矩形形状具有固定的最大高度和宽度(300,200),以及固定的最小高度和宽度(1.0,1.0)。矩形也可以移动,并且可以调整到最大和最小尺寸范围内的任何尺寸。

2 个答案:

答案 0 :(得分:3)

我只是部分理解你的问题,如果你想裁剪UIImage,请查看以下代码:

//Pass the UIImage object and the varying rectangle you "outputrect"
     - (UIImage *)cropImage:(UIImage *)image outPutRect:(CGRect) outputRect
    {

         CGImageRef takenCGImage = image.CGImage;
        size_t width = CGImageGetWidth(takenCGImage);
        size_t height = CGImageGetHeight(takenCGImage);
        CGRect cropRect = CGRectMake(outputRect.origin.x * width, outputRect.origin.y * height,
                                     outputRect.size.width * width, outputRect.size.height * height);

        CGImageRef cropCGImage = CGImageCreateWithImageInRect(takenCGImage, cropRect);
        image = [UIImage imageWithCGImage:cropCGImage scale:1 orientation:image.imageOrientation];
        CGImageRelease(cropCGImage);

        return image;
    }

答案 1 :(得分:1)

您可以根据TOCropViewController.m类的需求使用TimOliver / TOCropViewController并更改剪裁框架,

1:从https://github.com/TimOliver/TOCropViewController下载完整项目。

2:将TOCropViewController.h,TOCropViewController.m和类拖放到其他类中。

3:根据TOCropViewController.m类中的需求设置比例[方法[self setAspectRatioPreset:self.aspectRatioPreset animated:NO];