iOS 设置图片imageView圆角——对图片进行裁剪

以前设置图片圆角总是把imageView设置成圆形,然后设置maskToBounds为YES,其实这样处理很消耗性能,图片多了之后比较卡,最好将图片进行裁剪后显示;这里有个分类可以用:

UIImage+wiRoundedRectImage.h

#import <UIKit/UIKit.h>

@interface UIImage (wiRoundedRectImage)

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

@end

UIImage+wiRoundedRectImage.m

#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

调用方法:

 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

代码Demo:  http://files.cnblogs.com/files/A--G/RoundImageDemo.zip

其实github上有个提供对image多种处理的库:

UIImage+Resize 调整图片大小
GitHub:https://github.com/coryalder/UIImage_Resize
提供多种方法为图片设置透明度、圆角、裁剪、调整大小等:

 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/

参考链接:1. http://www.cnblogs.com/thefeelingofsimple/archive/2013/02/20/2918547.html

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

时间: 2024-10-07 19:32:38

iOS 设置图片imageView圆角——对图片进行裁剪的相关文章

iOS设置控件圆角,和label的行距。

1. 设置控件圆角 UIView *nameBgView = (UIView *)[PublicMethods creatViewWithType:@"UIView" andParent:name andTag:kDrawCarTabelNameHeadBgViewTag];            [nameBgView setFrame:CGRectMake(kDrawCarTableContentLabelXMargin, 30, name.frame.size.width-kDr

iOS设置图片的放大效果

比如在tableview的头部设置一张图片,下拉tableview,上面的图片会显示放大的效果. 首先定义UIImageView,设置相关参数 static CGFloat kImageOriginHight = 240.f; expandZoomImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, -kImageOriginHight, self.tableView.frame.size.width, kImageOrigin

iOS 设置控件圆角及边框

1. 设置圆角: self.view.layer.masksToBounds = YES; self.view.layer.cornerRadius = 10.0f; 2. 添加边框: self.layer.borderWidth = 5.0f; self.layer.borderColor = [[UIColor colorWithRed:164.0/255 green:142.0/255 blue:247.0/255 alpha:1.0] CGColor]; 示例:

IOS 将图片转换为圆角图

UIImage+wiRoundedRectImage.h #import <UIKit/UIKit.h> @interface UIImage (wiRoundedRectImage) + (id)createRoundedRectImage:(UIImage*)image size:(CGSize)size radius:(NSInteger)r; @end UIImage+wiRoundedRectImage.m #import "UIImage+wiRoundedRectIma

IOS设置图片背景

在UIViewController里面这样设置: self.view.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed:@"1.jpg"] ];

一起学android之设置资源图片为圆角图片 (28)

效果图: 参看以下代码: public class MainActivity extends Activity { private ImageView imageView1; private ImageView imageView2; Bitmap mBitmap; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.

iOS设置圆角及圆形图片

方方正正的样式往往会显得很生硬,而圆角的样式会让人产生别样的亲切感,现在越来越多地用到圆角,诸如用户头像之类的图片也大都用圆形来显示,本文就介绍如何设置按钮.文本框的圆角以及制作圆形的图片. 先来看看效果图: 如效果图所示,我们制作了一个圆形的头像,一个完全半圆的圆角按钮,一个小圆角的按钮,以及一个带边框且边框为圆角的label. 大概思路如下: View都有一个layer的属性,我们正是通过layer的一些设置来达到圆角的目的,因此诸如UIImageView.UIButton.UILabel等

设置图片圆角的三种方式

//了解切圆角的方式有三种,我们一种一种来看 //圆角的第一种方式:(设置Layer属性) UIImageView *imageView = [[UIImageView alloc]init]; imageView.backgroundColor = [UIColor purpleColor]; imageView.frame = CGRectMake(110, 100, 200, 200); imageView.image = [UIImage imageNamed:@"1"]; /

Android开发之自定义圆角矩形图片ImageView的实现

android中的ImageView只能显示矩形的图片,这样一来不能满足我们其他的需求,比如要显示圆角矩形的图片,这个时候,我们就需要自定义ImageView了,其原理就是首先获取到图片的Bitmap,然后进行裁剪对应的圆角矩形的bitmap,然后在onDraw()进行绘制圆角矩形图片输出. 效果图如下: 自定义的圆形的ImageView类的实现代码如下: package com.xc.xcskin.view; import android.content.Context; import and