IOS Coregraphic从相册绘制图片的方向处理

Coregraphic从相册导入图片并绘制时,方向会错误.我写了个方法,处理好了这个问题,上代码:

- (UIImage *)rotateImage:(UIImage *)image{

UIImageOrientation orient = image.imageOrientation;

switch (orient) {

case UIImageOrientationRight:

{

CGSize imgSize = CGSizeMake(image.size.width, image.size.height);

UIGraphicsBeginImageContext(imgSize);

CGContextRotateCTM(UIGraphicsGetCurrentContext(), M_PI / 2.0);

CGContextTranslateCTM(UIGraphicsGetCurrentContext(), 0, -imgSize.width);

CGContextDrawImage(UIGraphicsGetCurrentContext(),CGRectMake(0, 0, imgSize.height, imgSize.width), image.CGImage);

UIImage *after = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return after;

}

break;

case UIImageOrientationLeft:

{

CGSize imgSize = CGSizeMake(image.size.width, image.size.height);

UIGraphicsBeginImageContext(imgSize);

CGContextRotateCTM(UIGraphicsGetCurrentContext(), -M_PI / 2.0);

CGContextTranslateCTM(UIGraphicsGetCurrentContext(), -imgSize.height, 0);

CGContextScaleCTM(UIGraphicsGetCurrentContext(), 1.0, -1.0);

CGContextTranslateCTM(UIGraphicsGetCurrentContext(), 0.0, -imgSize.width);

CGContextDrawImage(UIGraphicsGetCurrentContext(),CGRectMake(0, 0, imgSize.height, imgSize.width), image.CGImage);

UIImage *after = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return after;

}

break;

case UIImageOrientationDown:

{

CGSize imgSize = CGSizeMake(image.size.width, image.size.height);

UIGraphicsBeginImageContext(imgSize);

CGContextScaleCTM(UIGraphicsGetCurrentContext(), -1.0, 1.0);

CGContextTranslateCTM(UIGraphicsGetCurrentContext(), -imgSize.width,0);

CGContextDrawImage(UIGraphicsGetCurrentContext(),CGRectMake(0, 0, imgSize.width, imgSize.height), image.CGImage);

UIImage *after = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return after;

}

break;

default:

break;

}

return image;

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容