//图片旋转,旋转用了绘图,可以保存后输出的完全方向变化的图片
- (UIImage *)image:(UIImage *)image rotation:(UIImageOrientation)orientation
{
longdouble rotate =0.0;
CGRect rect;
float translateX =0;
float translateY =0;
float scaleX =1.0;
float scaleY =1.0;
switch (orientation)
{
case UIImageOrientationLeft:
rotate = M_PI_2;
rect = CGRectMake(0,0, image.size.height, image.size.width);
translateX = 0;
translateY = -rect.size.width;
scaleY = rect.size.width/rect.size.height;
scaleX = rect.size.height/rect.size.width;
break;
case UIImageOrientationRight:
rotate = 3 *M_PI_2;
rect = CGRectMake(0,0, image.size.height, image.size.width);
translateX = -rect.size.height;
translateY = 0;
scaleY = rect.size.width/rect.size.height;
scaleX = rect.size.height/rect.size.width;
break;
case UIImageOrientationDown:
rotate = M_PI;
rect = CGRectMake(0,0, image.size.width, image.size.height);
translateX = -rect.size.width;
translateY = -rect.size.height;
break;
default:
rotate = 0.0;
rect = CGRectMake(0,0, image.size.width, image.size.height);
translateX = 0;
translateY = 0;
break;
}
UIGraphicsBeginImageContext(rect.size);
CGContextRef context =UIGraphicsGetCurrentContext();
//做CTM变换
CGContextTranslateCTM(context,0.0, rect.size.height);
CGContextScaleCTM(context,1.0, -1.0);
CGContextRotateCTM(context, rotate);
CGContextTranslateCTM(context, translateX, translateY);
CGContextScaleCTM(context, scaleX, scaleY);
//绘制图片
CGContextDrawImage(context,CGRectMake(0,0, rect.size.width, rect.size.height), image.CGImage);
UIImage *newPic =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newPic;
}
//缩略图,用UIImage来缩放,但是需要对缩放后的图片进行绘图保存,不然下次使用依然是未缩放的图片,其中的imageThumbnailScale方法,比如scale是10,则图片长宽都缩放5倍,即长宽都变为原来的0.2倍
UIImage *sImage = [UIImageimageWithCGImage:tLeftImage.CGImagescale:[tLeftImageimageThumbnailScale]orientation:tLeftImage.imageOrientation];
CGSize scaleSize =CGSizeMake(sImage.size.width , sImage.size.height);
UIGraphicsBeginImageContextWithOptions(scaleSize,NO,0.0f);
[sImage drawInRect:CGRectMake(0,0, scaleSize.width, scaleSize.height)];
UIImage *scaleLeftImage =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//其中imageThumbnailScale方法
-(CGFloat)imageThumbnailScale
{
CGFloat tw = (self.size.height>=self.size.width?self.size.width:self.size.height);
这个返回值,缩放后,图片的尺寸都变为400X400
return tw/200;
}
/**
* 根据UIImage生成方形缩略图
*
* @param aimage 传进来的图片
*
* @return 传出裁剪后的图片
*/
-(UIImage *)thumbnailWithImage:(UIImage *)aimage
{
CGRect irect;
CGFloat aw = aimage.size.width;
CGFloat ah = aimage.size.height;
if (aw >= ah)
{
irect.origin.x = (aw - ah)*0.5f;
irect.origin.y =0.0f;
irect.size.width = ah;
irect.size.height = ah;
}else
{
irect.origin.x =0.0f;
irect.origin.y = (ah - aw)*0.5f;
irect.size.width = aw;
irect.size.height = aw;
}
return [aimagecutImageWithRect:irect];
}
//cut方法
-(UIImage *)cutImageWithRect:(CGRect)cutRect{
CGSize cutSize = cutRect.size;
UIGraphicsBeginImageContextWithOptions(cutSize,NO,0.0f);
[selfdrawInRect:CGRectMake(cutRect.origin.x * -1.0f, cutRect.origin.y * -1.0f,self.size.width,self.size.height)];
UIImage *resultImage =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return resultImage;
}
//大图裁剪一部分区域,被弃用,消耗内存过大
-(UIImage *)getImageFromImage:(UIImage *)bigImage
{
//大图bigImage
//定义myImageRect,截图的区域
CGFloat iWidth;
if (bigImage.size.height>=bigImage.size.width)
{
iWidth = bigImage.size.width;
}else
{
iWidth = bigImage.size.height;
}
CGRect myImageRect =CGRectMake((bigImage.size.width-iWidth)/2, (bigImage.size.height-iWidth)/2, iWidth, iWidth);
CGImageRef imageRef = bigImage.CGImage;
CGImageRef subImageRef =CGImageCreateWithImageInRect(imageRef, myImageRect);
CGSize size;
size.width = iWidth;
size.height = iWidth;
UIGraphicsBeginImageContext(size);
CGContextRef context =UIGraphicsGetCurrentContext();
CGContextDrawImage(context, myImageRect, subImageRef);
UIImage* smallImage = [UIImageimageWithCGImage:subImageRef];
UIGraphicsEndImageContext();
return smallImage;
}
CoreGraphics对UIImage图片裁剪
最新推荐文章于 2019-09-29 11:08:25 发布