以椭圆形状裁剪图像

时间:2014-03-24 14:02:58

标签: iphone objective-c uiimage core-graphics

我在App上工作,我需要提供一个裁剪选项。一旦我从相机或图库中选择图像,它应该在编辑页面打开,我们有椭圆形图像,缩放和放大移动选项。一旦我们点击应用,捕获的图像应该以椭圆形状裁剪。

现在关注的屏幕来自鸟舍sdk。但它有方形裁剪和需要的是椭圆形的裁剪。我试图自定义但不能这样做。

enter image description here

任何人都可以建议我使用最简单或最合适的方式来实现这一目标。

感谢。

2 个答案:

答案 0 :(得分:1)

您可以执行此类屏蔽,或者您使用coco控件

.h档案: -

@interface yourClass : UIImageView

@end

.m档案: -

    #import <QuartzCore/QuartzCore.h>

    @implementation yourClass

    - (void)awakeFromNib
    {
        [super awakeFromNib];
        CALayer *mask = [CALayer layer];
        mask.contents = (id)[[UIImage imageNamed:@"ovalMask.png"] CGImage];
        CGSize size = self.frame.size;
        mask.frame = CGRectMake(0, 0, size.width, size.height);
        self.layer.mask = mask;
        [self.layer setMasksToBounds:YES];
    }

@end

答案 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;
}