iOS-邊框圖片,頭像邊框

來源:互聯網
上載者:User

標籤:

項目中偶爾會有帶邊框圖片的需求,或是頭像亦或是logo.在原型圖片外圍加一自訂色彩的邊框,思路是根據上下文繪製,寫了Demo和解釋.
帶邊框的圖片Demo:

-(void)borderImage{    //1.載入圖片    UIImage *image = [UIImage imageNamed:@"baby"];    //2.邊框寬度    CGFloat borderW = 10;    //3.開啟圖片上下文    CGSize size = CGSizeMake(image.size.width+2*borderW, image.size.height+2*borderW);    UIGraphicsBeginImageContextWithOptions(size, NO, 0);    //4.先描述一個大圓作為填充    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, size.width, size.height)];    [[UIColor redColor] set];    [path fill];    //5.在添加一個小圓設為剪裁地區    path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(borderW, borderW, image.size.width, image.size.height)];    [path addClip];    //6.把圖片繪製上下文    [image drawInRect:CGRectMake(borderW, borderW, image.size.width, image.size.height)];    //7.產生圖片    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();    //8.關閉上下文    UIGraphicsEndImageContext();    self.imageV.image = newImage;}

方便調用的UIIamge的分類:

/** *  帶邊框的圖片 * *  @param imageName   圖片名 *  @param imageWidth  圖片寬度 *  @param imageHeight 圖片高度 *  @param borderWidth 邊框寬度 *  @param borderColor 邊框顏色 * *  @return 帶邊框的圖片 */+(UIImage *)imageWithImageName:(NSString *)imageName imageWidth:(CGFloat)imageWidth imageHeight:(CGFloat)imageHeight borderWidth:(CGFloat)borderWidth borderColor:(UIColor *)borderColor{    UIImage *image = [UIImage imageNamed:imageName];    CGSize size = CGSizeMake(imageWidth + 2 * borderWidth, imageHeight + 2 * borderWidth);    UIGraphicsBeginImageContextWithOptions(size, NO, 0);    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, size.width, size.height)];    [borderColor set];    [path fill];    path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(borderWidth, borderWidth, imageWidth, imageHeight)];    [path addClip];    [image drawInRect:CGRectMake(borderWidth, borderWidth, imageWidth, imageHeight)];    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();    UIGraphicsEndImageContext();    return newImage;}

完整項目Demo連結:
https://github.com/qxuewei/XWBorderImage

iOS-邊框圖片,頭像邊框

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.