最近几个月一直在忙项目,项目昨天做了上线,今天的时间比较充足,对项目中的图片处理做个简单的总结,希望广大朋友们支持一下
完整Demo地址
OC版:https://github.com/GaoQianpu/QPImageToor_Obj-C
swif版:https://github.com/GaoQianpu/QPImageToor
1、改变image的size
CGRectrect =CGRectMake(0,0, size.width, size.height);
UIGraphicsBeginImageContextWithOptions(size,NO,2.0);
//关键步骤:在rect内将该图片画进去
[imagedrawInRect:rect];
UIImage* scaledImage =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
2、image的裁剪(此处我将图片居中裁剪成为正方形,大家可根据自己的需求调整)
此处swift和OC使用不同的API,但是原理还是相同的
注意!!在进行截图的时候自己一定要算对尺寸,因为当裁剪范围超出 图片范围后,得到的image为空
当然这里也可以用贝塞尔曲线,选择出自己需要的区域,然后再把这部分图像给画上去,这部分代码在后续写贝塞尔曲线的时候再写,这里就不赘述了
OC:
CGRectrect =CGRectZero;
//计算rect使截图居中
if(image.size.width>image.size.height) {
rect =CGRectMake((image.size.width-image.size.height)*0.5,0, image.size.height, image.size.height);
}else{
rect =CGRectMake(0, (image.size.height-image.size.width)*0.5, image.size.width, image.size.width);
}
CGImageRefsubImageRef =CGImageCreateWithImageInRect(image.CGImage, rect);
CGRectsmallBounds =CGRectMake(0,0,CGImageGetWidth(subImageRef),CGImageGetHeight(subImageRef));
UIGraphicsBeginImageContextWithOptions(smallBounds.size,NO,2.0);
CGContextRefcontext =UIGraphicsGetCurrentContext();
CGContextDrawImage(context, smallBounds, subImageRef);
UIImage* newImage = [UIImageimageWithCGImage:subImageRef];
UIGraphicsEndImageContext();
swift:
//因为在swif中,CGImageRef被取消,不过本质还是指image的CGImage
varpoint =CGPoint.zero
varsize =CGSize.zero
ifimage.size.width> image.size.height{
point =CGPoint(x: (image.size.width- image.size.height) *0.5, y:0)
size.height= image.size.height
size.width= image.size.height
}else{
point =CGPoint(x:0, y: (image.size.height- image.size.width) *0.5)
size.width= image.size.width
size.height= image.size.width
}
letimageRef = image.cgImage
letsubImageRef = imageRef!.cropping(to:CGRect(origin: point, size: size))
letnewImage =UIImage(cgImage: subImageRef!)
2、image的拉伸
正常情况下修改UIImageView,UIButton等控件的size,会随意拉伸其中的UIImage,使图片变形,如果图形全靠美工妹子切的话,那可怜的猿猿们该单身到多会儿啊,为了广大单身同志的幸福,大家肯定是愿意多写几行代码,而不是葬送自己撩到妹的机会,因为毕竟搞开发的平时很难接触到太多的妹子,你们说是不?
现在iOS已经基本上都在iOS 8以上了,所以iOS 8以前的API基本上用不到了,不过我还是把代码贴出来让大家一饱眼福
iOS 6以上
CGFloattop = image.size.height*0.5;
CGFloatleft = image.size.width*0.5;
CGFloatbottom = image.size.height*0.5;
CGFloatright = image.size.width*0.5;
UIEdgeInsetsedgeInsets =UIEdgeInsetsMake(top, left, bottom, right);
UIImageResizingModemode =UIImageResizingModeStretch;
newImage = [imageresizableImageWithCapInsets:edgeInsetsresizingMode:mode];
iOS 5 --- iOS 6
CGFloattop = image.size.height*0.5;
CGFloatleft = image.size.width*0.5;
CGFloatbottom = image.size.height*0.5;
CGFloatright = image.size.width*0.5;
UIEdgeInsetsedgeInsets =UIEdgeInsetsMake(top, left, bottom, right);
UIImageResizingModemode =UIImageResizingModeStretch;
newImage = [imageresizableImageWithCapInsets:edgeInsetsresizingMode:mode];
iOS 5以下
CGFloattop = image.size.height*0.5;
CGFloatleft = image.size.width*0.5;
CGFloatbottom = image.size.height*0.5;
CGFloatright = image.size.width*0.5;
UIEdgeInsetsedgeInsets =UIEdgeInsetsMake(top, left, bottom, right);
newImage = [imageresizableImageWithCapInsets:edgeInsets];
3、image的拼接
//拼接尺寸及坐标
UIImage* image1 =[UIImage imageNamed:@"1"];
UIImage* image2 =[UIImage imageNamed:@"2"];
CGRectrect1 =CGRectMake(0,0,image1.size.width,image1.size.height);
CGRectrect2 =CGRectMake(0,image1.size.height+5,image2.size.width, im2.size.height);
//拼接图片后的总大小
CGSizesize =CGSizeMake(200,image1.size.height+ im2.size.height+5);
UIGraphicsBeginImageContextWithOptions(size,false,2.0);
[image1drawInRect:rect1];//把第一张图画上去
[image2drawInRect:rect2];//把第二张画上去
UIImage* drawImage =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
最后,再给大家说下我的Demo下载地址
完整Demo地址