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;

}

你可能感兴趣的:(IOS Coregraphic从相册绘制图片的方向处理)