圖片切圓處理

新建UIImage的分類聲明發方法並實現

+(UIImage *)imageWithName:(NSString *)imageName imageBorder:(CGFloat)border borderColor:(UIColor *)color{

    //  設置生成切圓的  外圓環的寬度

    CGFloat circleBorder = border;

    UIImage *oldImage = [UIImageimageNamed:imageName];

    //    新圖片的尺寸

    CGFloat newImageWidth = oldImage.size.width+2*circleBorder;

    CGFloat newImageHeight = oldImage.size.height+2*circleBorder;

    CGFloat circleW =  (newImageWidth > newImageHeight) ? newImageHeight:newImageWidth;

    

    //    開啓上下文

    UIGraphicsBeginImageContextWithOptions(CGSizeMake(circleW, circleW),NO, 0.0);

    //    畫大圓

    UIBezierPath *pathBigCircle = [UIBezierPathbezierPathWithOvalInRect:CGRectMake(0,0, circleW, circleW)];

    //    獲取當前的上下文

    CGContextRef contextRef =UIGraphicsGetCurrentContext();

    //    添加到上下文

    CGContextAddPath(contextRef, pathBigCircle.CGPath);

    [color  set];

    //    渲染

    CGContextFillPath(contextRef);

    

    CGRect clip =CGRectMake(circleBorder, circleBorder, oldImage.size.width, oldImage.size.height);

    //    正切與OldImage的圓

    UIBezierPath *pathSmallCircle = [UIBezierPathbezierPathWithOvalInRect:clip];

    //    設置裁剪區域

    [pathSmallCircle addClip];

    

    //    畫圖片

    [oldImage drawAtPoint:CGPointMake(circleBorder, circleBorder)];

    //    生成新的圖片

    UIImage *newImage =UIGraphicsGetImageFromCurrentImageContext();

    

    UIGraphicsEndImageContext();

    

    return newImage;

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章