使用边框框架裁剪图像

时间:2014-03-31 06:55:05

标签: iphone objective-c uiimage

我正在尝试使用矩形框裁剪图像。但不知何故不能按照要求做到这一点。

这是我在尝试的内容:

enter image description here

这是我想要的结果:

enter image description here

现在我需要的是点击完成图像时应该将矩形形状裁剪为精确放置在图像中。我尝试过一些像masking& amp;使用蒙版图像rect绘制图像但尚未成功。

这是我的代码无效:

CALayer *mask = [CALayer layer];
mask.contents = (id)[imgMaskImage.image CGImage];
mask.frame = imgMaskImage.frame;
imgEditedImageView.layer.mask = mask;
imgEditedImageView.layer.masksToBounds = YES;

任何人都可以建议我实施它的更好方法。

我已经尝试了很多其他的东西&浪费时间所以,如果我得到一些帮助,那将是伟大的&赞赏。

感谢。

2 个答案:

答案 0 :(得分:1)

这是你的方式

+(UIImage *)maskImage:(UIImage *)image andMaskingImage:(UIImage *)maskingImage{
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGImageRef maskImageRef = [maskingImage CGImage];
    CGContextRef mainViewContentContext = CGBitmapContextCreate (NULL, maskingImage.size.width, maskingImage.size.height, 8, 0, colorSpace, kCGImageAlphaPremultipliedLast);
    if (mainViewContentContext==NULL)
        return NULL;
    CGFloat ratio = 0;
    ratio = maskingImage.size.width/ image.size.width;
    if(ratio * image.size.height < maskingImage.size.height) {
        ratio = maskingImage.size.height/ image.size.height;
    } 
    CGRect rect1  = {{0, 0}, {maskingImage.size.width, maskingImage.size.height}};
//// CHANGE THIS RECT ACCORDING TO YOUR NEEDS

        CGRect rect2  = {{-((image.size.width*ratio)-maskingImage.size.width)/2 , -((image.size.height*ratio)-maskingImage.size.height)/2}, {image.size.width*ratio, image.size.height*ratio}};
        CGContextClipToMask(mainViewContentContext, rect1, maskImageRef);
        CGContextDrawImage(mainViewContentContext, rect2, image.CGImage);
        CGImageRef newImage = CGBitmapContextCreateImage(mainViewContentContext);
        CGContextRelease(mainViewContentContext);
        CGColorSpaceRelease(colorSpace);
        UIImage *theImage = [UIImage imageWithCGImage:newImage];
        CGImageRelease(newImage);
        return theImage;
    }

你需要有这样的图像

请注意 遮罩图像不能具有任何透明度。相反,透明区域必须是白色或黑色和白色之间的某个值。像素越黑,它变得越不透明。

enter image description here

答案 1 :(得分:1)

- (UIImage *)croppedPhoto {
    // For dealing with Retina displays as well as non-Retina, we need to check
    // the scale factor, if it is available. Note that we use the size of teh cropping Rect
    // passed in, and not the size of the view we are taking a screenshot of.
         CGRect croppingRect = CGRectMake(imgMaskImage.frame.origin.x,
    imgMaskImage.frame.origin.y, imgMaskImage.frame.size.width,
    imgMaskImage.frame.size.height);

    imgMaskImage.hidden=YES;

    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
        UIGraphicsBeginImageContextWithOptions(croppingRect.size, YES,
    [UIScreen mainScreen].scale);
    } else {
        UIGraphicsBeginImageContext(croppingRect.size);
    }

    // Create a graphics context and translate it the view we want to crop so
    // that even in grabbing (0,0), that origin point now represents the actual
    // cropping origin desired:
         CGContextRef ctx = UIGraphicsGetCurrentContext();
         CGContextTranslateCTM(ctx, -croppingRect.origin.x, -croppingRect.origin.y);
    [self.view.layer renderInContext:ctx];

    // Retrieve a UIImage from the current image context:
    UIImage *snapshotImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    // Return the image in a UIImageView:
    return snapshotImage;
}
相关问题