天天看點

Quartz2D-圓形圖檔剪切

Quartz2D-圓形圖檔剪切

實作流程:在上下文繪制一個剪切後的圓;然後再将圖檔繪制上去;最後通過從上下文中取出這張圖檔

//加載剪切圖檔
    UIImage *image = [UIImage imageNamed:@"阿狸頭像"];
    //1.擷取上下文
    UIGraphicsBeginImageContextWithOptions(image.size, NO, 0);
    
    //2.繪制剪切路徑----圓
    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
    [path addClip];
    
    //3.繪制圖檔
    [image drawAtPoint:CGPointZero];
    
    //4.從上下文中取出剪切後的圖檔
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    
    //5.關閉上下文
    UIGraphicsEndImageContext();
    
    self.imageView.image = newImage;