目前使用过的图片裁剪方法
1、等比例压缩 裁剪出的图片是以asize最小值为边框的正方形图片
//修改图片尺寸 同比缩放
+ (UIImage *)thumbnailWithImageWithoutScale:(UIImage *)image size:(CGSize)asize
{
UIImage *newimage;
if (nil == image) {
newimage = nil;
}
else{
CGSize oldsize = image.size;
CGRect rect;
if (asize.width/asize.height > oldsize.width/oldsize.height) {
rect.size.width = asize.height*oldsize.width/oldsize.height;
rect.size.height = asize.height;
rect.origin.x = (asize.width - rect.size.width)/2;
rect.origin.y = 0;
}
else{
rect.size.width = asize.width;
rect.size.height = asize.width*oldsize.height/oldsize.width;
rect.origin.x = 0;
rect.origin.y = (asize.height - rect.size.height)/2;
}
UIGraphicsBeginImageContext(asize);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]);
UIRectFill(CGRectMake(0, 0, asize.width, asize.height));//clear background
[image drawInRect:rect];
newimage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
return newimage;
}
2、简单粗暴地图片裁剪方法 裁剪出的图片尺寸按照size的尺寸,但图片可能会被拉伸
+ (UIImage *)thumbnailWithImage:(UIImage *)originalImage size:(CGSize)size
{
UIImage *newimage;
if (nil == image) {
newimage = nil;
}
else{
UIGraphicsBeginImageContext(asize);
[image drawInRect:CGRectMake(0, 0, asize.width, asize.height)];
newimage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
return newimage;
}
3、裁剪出的图片尺寸按照size的尺寸,但图片不拉伸,但多余部分会被裁减掉
+ (UIImage *)thumbnailWithImage:(UIImage *)originalImage size:(CGSize)size
{
CGSize originalsize = [originalImage size];
//原图长宽均小于标准长宽的,不作处理返回原图
if (originalsize.width<size.width && originalsize.height<size.height)
{
return originalImage;
}
//原图长宽均大于标准长宽的,按比例缩小至最大适应值
else if(originalsize.width>size.width && originalsize.height>size.height)
{
CGFloat rate = 1.0;
CGFloat widthRate = originalsize.width/size.width;
CGFloat heightRate = originalsize.height/size.height;
rate = widthRate>heightRate?heightRate:widthRate;
CGImageRef imageRef = nil;
if (heightRate>widthRate)
{
imageRef = CGImageCreateWithImageInRect([originalImage CGImage], CGRectMake(0, originalsize.height/2-size.height*rate/2, originalsize.width, size.height*rate));//获取图片整体部分
}
else
{
imageRef = CGImageCreateWithImageInRect([originalImage CGImage], CGRectMake(originalsize.width/2-size.width*rate/2, 0, size.width*rate, originalsize.height));//获取图片整体部分
}
UIGraphicsBeginImageContext(size);//指定要绘画图片的大小
CGContextRef con = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(con, 0.0, size.height);
CGContextScaleCTM(con, 1.0, -1.0);
CGContextDrawImage(con, CGRectMake(0, 0, size.width, size.height), imageRef);
UIImage *standardImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGImageRelease(imageRef);
return standardImage;
}
//原图长宽有一项大于标准长宽的,对大于标准的那一项进行裁剪,另一项保持不变
else if(originalsize.height>size.height || originalsize.width>size.width)
{
CGImageRef imageRef = nil;
if(originalsize.height>size.height)
{
imageRef = CGImageCreateWithImageInRect([originalImage CGImage], CGRectMake(0, originalsize.height/2-size.height/2, originalsize.width, size.height));//获取图片整体部分
}
else if (originalsize.width>size.width)
{
imageRef = CGImageCreateWithImageInRect([originalImage CGImage], CGRectMake(originalsize.width/2-size.width/2, 0, size.width, originalsize.height));//获取图片整体部分
}
UIGraphicsBeginImageContext(size);//指定要绘画图片的大小
CGContextRef con = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(con, 0.0, size.height);
CGContextScaleCTM(con, 1.0, -1.0);
CGContextDrawImage(con, CGRectMake(0, 0, size.width, size.height), imageRef);
UIImage *standardImage = UIGraphicsGetImageFromCurrentImageContext();
NSLog(@"改变后图片的宽度为%f,图片的高度为%f",[standardImage size].width,[standardImage size].height);
UIGraphicsEndImageContext();
CGImageRelease(imageRef);
return standardImage;
}
//原图为标准长宽的,不做处理
else
{
return originalImage;
}
}
原文:http://www.cnblogs.com/huaixu/p/6273681.html