美文网首页
IOS Coregraphic从相册绘制图片的方向处理

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

作者: TownKing | 来源:发表于2017-11-05 12:15 被阅读13次

    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从相册绘制图片的方向处理

          本文链接:https://www.haomeiwen.com/subject/somcmxtx.html