iOS 設置圖片imageView圓角——對圖片進行裁剪

 

之前設置圖片圓角老是把imageView設置成圓形,而後設置maskToBounds爲YES,其實這樣處理很消耗性能,圖片多了以後比較卡,最好將圖片進行裁剪後顯示;這裏有個分類能夠用:html

UIImage+wiRoundedRectImage.hgit

複製代碼
#import <UIKit/UIKit.h>

@interface UIImage (wiRoundedRectImage)

+ (id)createRoundedRectImage:(UIImage*)image size:(CGSize)size radius:(NSInteger)r;

@end
複製代碼

 

UIImage+wiRoundedRectImage.mgithub

複製代碼
#import "UIImage+wiRoundedRectImage.h"

@implementation UIImage (wiRoundedRectImage)

static void addRoundedRectToPath(CGContextRef context, CGRect rect, float ovalWidth,
                                 float ovalHeight)
{
    float fw, fh;
    
    if (ovalWidth == 0 || ovalHeight == 0)
    {
        CGContextAddRect(context, rect);
        return;
    }
    
    CGContextSaveGState(context);
    CGContextTranslateCTM(context, CGRectGetMinX(rect), CGRectGetMinY(rect));
    CGContextScaleCTM(context, ovalWidth, ovalHeight);
    fw = CGRectGetWidth(rect) / ovalWidth;
    fh = CGRectGetHeight(rect) / ovalHeight;
    
    CGContextMoveToPoint(context, fw, fh/2);  // Start at lower right corner
    CGContextAddArcToPoint(context, fw, fh, fw/2, fh, 1);  // Top right corner
    CGContextAddArcToPoint(context, 0, fh, 0, fh/2, 1); // Top left corner
    CGContextAddArcToPoint(context, 0, 0, fw/2, 0, 1); // Lower left corner
    CGContextAddArcToPoint(context, fw, 0, fw, fh/2, 1); // Back to lower right
    
    CGContextClosePath(context);
    CGContextRestoreGState(context);
}

+ (id)createRoundedRectImage:(UIImage*)image size:(CGSize)size radius:(NSInteger)r
{
    // the size of CGContextRef
    int w = size.width;
    int h = size.height;
    
    UIImage *img = image;
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
    CGRect rect = CGRectMake(0, 0, w, h);
    
    CGContextBeginPath(context);
    addRoundedRectToPath(context, rect, r, r);
    CGContextClosePath(context);
    CGContextClip(context);
    CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage);
    CGImageRef imageMasked = CGBitmapContextCreateImage(context);
    img = [UIImage imageWithCGImage:imageMasked];
    
    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);
    CGImageRelease(imageMasked);
    
    return img;
}

@end
複製代碼

 

調用方法:post

 

 1     UIImage * image = [UIImageimageNamed:@"123.jpg"];  // 設置原圖
 2 
 3     CGSize size = CGSizeMake(100,100);  // 設置尺寸
 4 
 5     UIImageView *testImageView = [[UIImageView alloc] init];
 6 
 7     testImageView.frame = CGRectMake(30, 30, imageWidth, imageWidth);
 8 
 9     testImageView.backgroundColor = [UIColor lightGrayColor];
10 
11     testImageView.contentMode = UIViewContentModeScaleAspectFit;
12 
13     [self.view addSubview:testImageView];
14 
15     testImageView.image = [UIImagecreateRoundedRectImage:image size:size radius:10];   // 設置radius

 

 

 

其實github上有個提供對image多種處理的庫:性能

UIImage+Resize 調整圖片大小
GitHub:https://github.com/coryalder/UIImage_Resize
提供多種方法爲圖片設置透明度、圓角、裁剪、調整大小等:ui

 1 - (UIImage *)imageWithAlpha;
 2 - (UIImage *)transparentBorderImage:(NSUInteger)borderSize;
 3 - (UIImage *)roundedCornerImage:(NSInteger)cornerSize borderSize:(NSInteger)borderSize;
 4 - (UIImage *)croppedImage:(CGRect)bounds;
 5 - (UIImage *)thumbnailImage:(NSInteger)thumbnailSize
 6           transparentBorder:(NSUInteger)borderSize
 7                cornerRadius:(NSUInteger)cornerRadius
 8        interpolationQuality:(CGInterpolationQuality)quality;
 9 - (UIImage *)resizedImage:(CGSize)newSize
10      interpolationQuality:(CGInterpolationQuality)quality;
11 - (UIImage *)
12   resizedImageWithContentMode:(UIViewContentMode)contentMode
13                        bounds:(CGSize)bounds
14          interpolationQuality:(CGInterpolationQuality)quality;

更詳細使用見:http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/ spa

 

參考連接:1. http://www.cnblogs.com/thefeelingofsimple/archive/2013/02/20/2918547.htmlcode

     2. http://www.cnblogs.com/A--G/p/4779759.htmlhtm

相關文章
相關標籤/搜索