9

用户可以更改在编辑屏幕中显示默认的裁剪框大小。我尝试使用以下代码:

- (UIImage *)imageByCropping:(UIImage *)imageToCrop toRect:(CGRect)rect {

    CGImageRef imageRef = CGImageCreateWithImageInRect([imageToCrop CGImage], rect);
    UIImage *cropped = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef); 
    return cropped;
}

但它裁剪了固定区域。如何裁剪用户选择的区域?

4

2 回答 2

28

对于获取裁剪图像:

UIImage *croppedImg = nil;
CGRect cropRect = CGRectMake("AS YOu Need"); //set your rect size.
croppedImg = [self croppIngimageByImageName:self.imageView.image toRect:cropRect];

使用以下代码调用 croppIngimageByImageName:toRect:返回的方法UIImage具有特定大小的图像

- (UIImage *)croppIngimageByImageName:(UIImage *)imageToCrop toRect:(CGRect)rect
    {
        //CGRect CropRect = CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height+15);

        CGImageRef imageRef = CGImageCreateWithImageInRect([imageToCrop CGImage], rect);
        UIImage *cropped = [UIImage imageWithCGImage:imageRef];
        CGImageRelease(imageRef);

        return cropped;
    }
于 2013-08-13T10:27:47.313 回答
8
    CGRect clippedRect  = CGRectMake(0 ,0,180 ,180);
    CGImageRef imageRef = CGImageCreateWithImageInRect(imgVw1.image.CGImage, clippedRect);
    UIImage *newImage   = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);
    imgVw1Cliped.image=newImage;

    NSLog(@"%d",imgVw1Cliped.image.imageOrientation);
于 2013-08-13T10:08:11.660 回答