简体   繁体   中英

get circular part of image

i am trying to imitate the new contact ios7 behavior.

I have a UIScrollView with a UImageView inside.

With this code i can draw the circular shape in front of the image.

-(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];
}

I need to get the portion of the image that is inside the circle when the user choose "Use Photo" option.

在此处输入图片说明

I wrote this category

@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

The following image was cropped twice with the same path. once inverted, once not inverted. than I merged them again.

在此处输入图片说明

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];

It is on Github

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM