簡體   English   中英

獲取圖像的圓形部分

[英]get circular part of image

我正在嘗試模仿新的聯系人 ios7 行為。

我有一個 UIScrollView,里面有一個 UImageView。

使用此代碼,我可以在圖像前面繪制圓形。

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

當用戶選擇“使用照片”選項時,我需要獲取圓圈內的圖像部分。

在此處輸入圖片說明

我寫了這個類別

@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

下圖用相同的路徑裁剪了兩次。 一次倒置,一次不倒置。 比我再次合並它們。

在此處輸入圖片說明

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 上

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM