获得图像的圆形部分

时间:2014-04-08 22:46:19

标签: ios objective-c image cocoa-touch

我正在尝试模仿新的联系人ios7行为。

我有一个内部有UImageView的UIScrollView。

使用此代码,我可以在图像前面绘制圆形。

-(void)viewWillAppear:(BOOL)animated
{
    CGFloat screenHeight = [[UIScreen mainScreen] bounds].size.height;

    int position = 0;

    if (screenHeight == 568)
    {
        position = 124;
    }
    else
    {
        position = 80;
    }

    CAShapeLayer *circleLayer = [CAShapeLayer layer];

    UIBezierPath *path2 = [UIBezierPath bezierPathWithOvalInRect:
                           CGRectMake(0.0f, position, 320.0f, 320.0f)];
    [path2 setUsesEvenOddFillRule:YES];

    [circleLayer setPath:[path2 CGPath]];

    [circleLayer setFillColor:[[UIColor clearColor] CGColor]];
    path;

    path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 320, screenHeight-44) cornerRadius:0];


    [path appendPath:path2];
    [path setUsesEvenOddFillRule:YES];

    fillLayer = [CAShapeLayer layer];
    fillLayer.path = path.CGPath;
    fillLayer.fillRule = kCAFillRuleEvenOdd;
    fillLayer.fillColor = [UIColor blackColor].CGColor;
    fillLayer.opacity = 0.8;
    [self.view.layer addSublayer:fillLayer];

    UILabel *moveLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 10, 320, 50)];
    [moveLabel setText:@"Mueva y escale"];
    [moveLabel setTextAlignment:NSTextAlignmentCenter];
    [moveLabel setTextColor:[UIColor whiteColor]];
    [self.view addSubview:moveLabel];
}

当用户选择"使用照片"时,我需要获取圆圈内的图像部分。选项。

enter image description here

1 个答案:

答案 0 :(得分:0)

我写了这个类别

@implementation UIImage (PathCropping)
-(UIImage *)imageCroppedWithPath:(UIBezierPath *)path
{

    return [self imageCroppedWithPath:path invertPath:NO];
}

-(UIImage *)imageCroppedWithPath:(UIBezierPath *)path
                      invertPath:(BOOL)invertPath
{
    float scaleFactor = [self scale];
    CGRect imageRect = CGRectMake(0, 0, self.size.width * scaleFactor, self.size.height *scaleFactor);

    CGColorSpaceRef colorSpace  = CGImageGetColorSpace(self.CGImage);
    CGContextRef context        = CGBitmapContextCreate(NULL,
                                                        imageRect.size.width,
                                                        imageRect.size.height ,
                                                        CGImageGetBitsPerComponent(self.CGImage),
                                                        0,
                                                        colorSpace,
                                                        (CGBitmapInfo)CGImageGetAlphaInfo(self.CGImage)
                                                        );
    CGContextSaveGState(context);

    CGContextTranslateCTM(context, 0.0, self.size.height *scaleFactor);
    CGContextScaleCTM(context, 1.0 , -1.0);

    CGAffineTransform transform = CGAffineTransformMakeScale(scaleFactor, scaleFactor);
    [path applyTransform:transform];

    if(invertPath){
        UIBezierPath *rectPath = [UIBezierPath bezierPathWithRect:imageRect];
        CGContextAddPath(context, rectPath.CGPath);
        CGContextAddPath(context, path.CGPath);
        CGContextEOClip(context);
    } else {
        CGContextAddPath(context, path.CGPath);
        CGContextClip(context);
    }

    CGContextScaleCTM(context, 1.0, -1.0);
    CGContextTranslateCTM(context, 0.0, -self.size.height * scaleFactor);

    CGContextDrawImage(context, imageRect, [self CGImage]);
    CGImageRef imageRef = CGBitmapContextCreateImage(context);
    return [UIImage imageWithCGImage:imageRef scale:scaleFactor orientation:0];
}
@end

使用相同的路径裁剪以下图像两次。一旦倒置,一旦不倒置。比我再次合并它们。

enter image description here

NSString *imageName = @"IMG_2345.jpg";

UIImage *image  = [UIImage imageNamed:imageName];
UIImage *image2 = [UIImage imageNamed:imageName];

CGSize size = CGSizeMake(320, 320 * image.size.height / image.size.width);
image  = [image  resizedImage:size interpolationQuality:kCGInterpolationHigh];
image2 = [image2 resizedImage:size interpolationQuality:kCGInterpolationHigh];
image2 = [image2 grayscaledImage];

image  = [image  imageCroppedWithPath:[self circlePathWithSize: circleSize] invertPath: NO];
image2 = [image2 imageCroppedWithPath:[self circlePathWithSize: circleSize] invertPath: YES];

它位于Github