CGImageCreateWithImageInRect与MZCroppableView rect问题

时间:2014-10-03 10:48:13

标签: ios uiimage

我正在使用此library从适当的路径裁剪图像。

似乎它很完美,但修改后图像有另一种尺寸,我无法理解如何控制。

我添加了与容器大小相同的图像。尺寸= 320 x 524

源中有一种返回裁剪图像的方法:

- (UIImage *)deleteBackgroundOfImage:(UIImageView *)image
{
    NSArray *points = [self.croppingPath points];

    CGRect rect = CGRectZero;
    rect.size = image.image.size;


    UIBezierPath *aPath;
    UIGraphicsBeginImageContextWithOptions(rect.size, YES, 0.0);
    {
        [[UIColor blackColor] setFill];
        UIRectFill(rect);
        [[UIColor whiteColor] setFill];

        aPath = [UIBezierPath bezierPath];

        // Set the starting point of the shape.
        CGPoint p1 = [MZCroppableView convertCGPoint:[[points objectAtIndex:0] CGPointValue] fromRect1:image.frame.size toRect2:image.image.size];
        [aPath moveToPoint:CGPointMake(p1.x, p1.y)];

        for (uint i=1; i<points.count; i++)
        {
            CGPoint p = [MZCroppableView convertCGPoint:[[points objectAtIndex:i] CGPointValue] fromRect1:image.frame.size toRect2:image.image.size];
            [aPath addLineToPoint:CGPointMake(p.x, p.y)];
        }
        [aPath closePath];
        [aPath fill];
    }

    UIImage *mask = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0.0);

    {
        CGContextClipToMask(UIGraphicsGetCurrentContext(), rect, mask.CGImage);
        [image.image drawAtPoint:CGPointZero];
    }

    UIImage *maskedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    CGRect croppedRect = aPath.bounds;
    croppedRect.origin.y = rect.size.height - CGRectGetMaxY(aPath.bounds);//This because mask become inverse of the actual image;

    croppedRect.origin.x = croppedRect.origin.x*2;
    croppedRect.origin.y = croppedRect.origin.y*2;
    croppedRect.size.width = croppedRect.size.width*2;
    croppedRect.size.height = croppedRect.size.height*2;

    CGImageRef imageRef = CGImageCreateWithImageInRect(maskedImage.CGImage, croppedRect);

    maskedImage = [UIImage imageWithCGImage:imageRef];

    return maskedImage;
}

当我在maskedImage = [UIImage imageWithCGImage:imageRef];行之前调试maskedImage时,似乎该方法删除背景正确,但由于某种原因,在方法CGImageCreateWithImageInRect(maskedImage.CGImage, croppedRect);之后修改了裁剪图像的帧。我试图在CGImageCreateWithImageInRect中手动设置裁剪的rect来裁剪所需的帧。

CGImageRef imageRef = CGImageCreateWithImageInRect(maskedImage.CGImage,CGRectMake(0,0,320,100));

但它对我没有任何回报。

如何提取适当的rect?

此行也乘以rect x2:

    croppedRect.origin.x = croppedRect.origin.x*2;
    croppedRect.origin.y = croppedRect.origin.y*2;
    croppedRect.size.width = croppedRect.size.width*2;
    croppedRect.size.height = croppedRect.size.height*2;

所以当我调试它时:

origin =(x = 242,y = 874)size =(width = 191,height = 164)

因此,您可以看到我的图像高度大小为524的Y点,但由于某种原因它可以正常工作。

0 个答案:

没有答案