UIImage的扩展方法——裁剪图片和等比列缩放图片-翻转-圆形等

本文详细介绍了如何使用扩展方法在iOS开发中对UIImage进行操作,包括裁剪图片以保持比例、等比例缩放、翻转图像以及创建圆形图片的方法,为iOS应用的UI设计提供便利。
摘要由CSDN通过智能技术生成
@interface UIImage (extend)


//按比例改变图片大小
-(UIImage*)changeImageSizeWithOriginalImage:(UIImage*)image percent:(float)percent;
//圆形
-(UIImage*)circleImage:(UIImage*)image;
//截取部分图像
-(UIImage*)getSubImage:(CGRect)rect;

//等比例缩放
-(UIImage*)scaleToSize:(CGSize)size;

-(UIImage *)rotateImage:(UIImage *)aImage with:(UIImageOrientation)theorient;

-(UIImage *)fixOrientation:(UIImage *)aImage;
@end

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);
}


@implementation UIImage (extend)



//按比例改变图片大小
-(UIImage*)changeImageSizeWithOriginalImage:(UIImage*)image percent:(float)percent
{
    // change the image size
	UIImage *changedImage=nil;
	float iwidth=image.size.width*percent;
	float iheight=image.size.height*percent;
	if (image.size.width != iwidth && image.size.height != iheight)
	{
        CGSize itemSize = CGSizeMake(iwidth, iheight);
		UIGraphicsBeginImageContext(itemSize);
		CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
		[image drawInRect:imageRect];
		changedImage = UIGraphicsGetImageFromCurrentImageContext();
		UIGraphicsEndImageContext();
    }
    else
    {
        changedImage = image;
    }
	
	return changedImage;
}
//圆角
-(UIImage *) createRoundedRectImage:(UIImage*)image size:(CGSize)size roundSize:(CGSize)roundSize
{
    // the size of CGContextRef
    int w = size.width;
    int h = size.height;
    
    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, roundSize.width, roundSize.height);
    CGContextClosePath(context);
    CGContextClip(context);
    CGContextDrawImage(context, CGRectMake(0, 0, w, h), image.CGImage);
    CGImageRef imageMasked = CGBitmapContextCreateImage(context);
    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);
    UIImage *image2 = [UIImage imageWithCGImage:imageMasked];
    CGImageRelease(imageMasked);
    return image2;
}

//圆形
-(UIImage*)circleImage:(UIImage*)image
{
    CGFloat inset = 0.1f;
    UIGraphicsBeginImageContext(image.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, 2);
    CGContextSetStrokeColorWithColor(context, [UIColor clearColor].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;
}

//将image按照theSize大小等比缩放并裁剪
-(UIImage*
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值