2014-04-18 203 views
2

我想用羽化圓圈裁剪圖像。我用它來裁剪圖像,但它只是裁剪正方形。ios - 用羽化邊緣裁剪圖像

CGImageRef imref = CGImageCreateWithImageInRect([newImage CGImage], faceRect); 
newSubImage = [UIImage imageWithCGImage:imref]; 

我想要的是用羽邊進行裁剪?我應該用什麼來實現它?

+0

看看這可以幫助http://stackoverflow.com/questions/17161088/how-to-refine-or-blur-or-smooth-只有一個只是這邊緣/ 17175381#17175381 – iphonic

+0

嘿,這是什麼樣的作物? SomeGuy的回答看起來不錯!他得到了它,否則就會有譁變的恩惠! – clearlight

回答

0

這個怎麼樣...首輪圖像:

//round the image 

UIImageView *roundView = [[UIImageView alloc] initWithImage:smallImage]; 
UIGraphicsBeginImageContextWithOptions(roundView.bounds.size, NO, [UIScreen mainScreen].scale); 
[[UIBezierPath bezierPathWithRoundedRect:roundView.bounds 
          cornerRadius:roundView.frame.size.width/2] addClip]; 
[smallImage drawInRect:roundView.bounds]; 
UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

,然後再圓,但具有更小的圓圈,通過調整阿爾法再次設置貝塞爾路徑顏色爲白色,但使其半透明...

3

這個片段將創建一個圓形切口與薄邊邊緣的

result

featherLocations變量可能是你需要調整

- (UIImage *) featheredImageWithImage:(UIImage *) image 
{ 
    // Locations of where the feather starts and ends (0 -> 1) 
    const CGFloat featherLocations[] = {0.9, 1}; 

    UIGraphicsBeginImageContextWithOptions(image.size, NO, image.scale); 

    CGContextRef ctx = UIGraphicsGetCurrentContext(); 

    // Draw the original image 
    [image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)]; 

    // A 'knock-out' gradient is used to generate a feather effect, 
    // the alpha channel on the colors defines the alpha of the drawn image 
    NSArray *gradientColors = @[(id)[UIColor colorWithWhite:0 alpha:1].CGColor, 
           (id)[UIColor colorWithWhite:0 alpha:0].CGColor]; 

    CGGradientRef gradient = CGGradientCreateWithColors(CGImageGetColorSpace(image.CGImage), (__bridge CFArrayRef)gradientColors, featherLocations); 

    // Because we're changing the draw mode below, 
    // take a snapshot of the current draw settings so we can reset them after 
    CGContextSaveGState(ctx); 

    // The kCGBlendModeDestinationIn blend mode will provide a'knock-out' effect on 
    // the previously drawn content, using the alpha channels of the gradient's colors 
    CGContextSetBlendMode(ctx, kCGBlendModeDestinationIn); 

    const CGPoint gradientCenter = CGPointMake(image.size.width/2, image.size.height/2); 

    // The gradient will start at the center (0) and extend to the closest edge (horizontal or vertical) 
    const CGFloat startRadius = 0; 
    const CGFloat endRadius = MIN(image.size.width, 
            image.size.height)/2; 

    // Draw the gradient to eliminate the pixels we don't want 
    CGContextDrawRadialGradient(ctx, gradient, gradientCenter, startRadius, gradientCenter, endRadius, (kCGGradientDrawsAfterEndLocation)); 

    CGGradientRelease(gradient); 
    gradient = NULL; 

    // Finally, restore state 
    // (note that in this example CGContextSaveGState and CGContextRestoreGState 
    // are optional because no further drawing happens after this point) 
    CGContextRestoreGState(ctx); 

    // Get the UIImage version 
    UIImage *featheredImage = UIGraphicsGetImageFromCurrentImageContext(); 

    UIGraphicsEndImageContext(); 

    return featheredImage; 
} 
+0

任何機會我們可以做到這一點,而不使用陰影和任何顏色漸變?我想要我的圖像的邊緣(由自定義的uibezierpath定義)從圖像淡入淡出,我不希望它的邊框上有陰影或漸變,這可能嗎?非常感謝。 –