iOS 簡單畫圓

一、經過image mask來操做,須要添加mask目標圖片。
二、經過imageview的layer來操做
以下代碼
UIImageView * imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image.png"]];  
   imageView.frame = CGRectMake(20.f, 20.f, 100.f, 100.f);  
   imageView.layer.masksToBounds = YES;  
   imageView.layer.cornerRadius = 50;  

a.這種方法須要添加QuarztCore框架才能操做
b.cornerradus的肯定問題
三、能過代碼對畫布裁剪成圓形–》而後再將原始圖像畫出來–》

-(UIImage*) circleImage:(UIImage*) image withParam:(CGFloat) inset {  
    UIGraphicsBeginImageContext(image.size);  
    CGContextRef context = UIGraphicsGetCurrentContext();  
    CGContextSetLineWidth(context, 2);  
    CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);  
    CGRect rect = CGRectMake(inset, inset, image.size.width - inset * 2.0f, image.size.height - inset * 2.0f);  
    CGContextAddEllipseInRect(context, rect);  
    CGContextClip(context);  
      
    [image drawInRect:rect];  
    CGContextAddEllipseInRect(context, rect);  
    CGContextStrokePath(context);  
    UIImage *newimg = UIGraphicsGetImageFromCurrentImageContext();  
    UIGraphicsEndImageContext();  
    return newimg;  
}  

上面代碼注意 若是裁剪後沒有使用 CGContextAddEllipseInRect(context, rect);
CGContextStrokePath(context); 這條代碼 就會引發背景爲白色時看不出來任務效果。
這裏是橢圓操做
相關文章
相關標籤/搜索