zoukankan      html  css  js  c++  java
  • iOS开发—— UIImage数据的保存

      1、保存图片和方向

      [userDefault setObject:UIImagePNGRepresentation(image) forKey:udUserImage];

      [userDefault setObject:[NSNumber numberWithInteger:image.imageOrientation] forKey:udUserImageOrientation];

      2、获取图片和方向

      NSData* imageData = [userDefault objectForKey:udUserImage];

      UIImage *userImage = [UIImage imageWithData:imageData];

      NSInteger imageOrientation = [[userDefault objectForKey:udUserImageOrientation] integerValue];

      3、显示图片

      imageView.image = [self fixOrientationWithImage:userImage AndImageOrientation:imageOrientation];

    - (UIImage *)fixOrientationWithImage:(UIImage *)aImage AndImageOrientation:(NSInteger)imageOrientation {

        if (imageOrientation == UIImageOrientationUp)

            return aImage;

        

        CGAffineTransform transform = CGAffineTransformIdentity;

        

        switch (imageOrientation) {

            case UIImageOrientationDown:

            case UIImageOrientationDownMirrored:

                transform = CGAffineTransformTranslate(transform, aImage.size.width, aImage.size.height);

                transform = CGAffineTransformRotate(transform, M_PI);

                break;

                

            case UIImageOrientationLeft:

            case UIImageOrientationLeftMirrored:

                transform = CGAffineTransformTranslate(transform, aImage.size.width, 0);

                transform = CGAffineTransformRotate(transform, M_PI_2);

                break;

                

            case UIImageOrientationRight:

            case UIImageOrientationRightMirrored:

                transform = CGAffineTransformTranslate(transform, 0, aImage.size.height);

                transform = CGAffineTransformRotate(transform, -M_PI_2);

                break;

            default:

                break;

        }

        

        switch (imageOrientation) {

            case UIImageOrientationUpMirrored:

            case UIImageOrientationDownMirrored:

                transform = CGAffineTransformTranslate(transform, aImage.size.width, 0);

                transform = CGAffineTransformScale(transform, -1, 1);

                break;

                

            case UIImageOrientationLeftMirrored:

            case UIImageOrientationRightMirrored:

                transform = CGAffineTransformTranslate(transform, aImage.size.height, 0);

                transform = CGAffineTransformScale(transform, -1, 1);

                break;

            default:

                break;

        }

        

        CGContextRef ctx = CGBitmapContextCreate(NULL, aImage.size.width, aImage.size.height,

                                                 CGImageGetBitsPerComponent(aImage.CGImage), 0,

                                                 CGImageGetColorSpace(aImage.CGImage),

                                                 CGImageGetBitmapInfo(aImage.CGImage));

        CGContextConcatCTM(ctx, transform);

        switch (imageOrientation) {

            case UIImageOrientationLeft:

            case UIImageOrientationLeftMirrored:

            case UIImageOrientationRight:

            case UIImageOrientationRightMirrored:

                // Grr...

                CGContextDrawImage(ctx, CGRectMake(0,0,aImage.size.height,aImage.size.width), aImage.CGImage);

                break;

                

            default:

                CGContextDrawImage(ctx, CGRectMake(0,0,aImage.size.width,aImage.size.height), aImage.CGImage);

                break;

        }

        

        CGImageRef cgimg = CGBitmapContextCreateImage(ctx);

        UIImage *img = [UIImage imageWithCGImage:cgimg];

        CGContextRelease(ctx);

        CGImageRelease(cgimg);

        return img;

    }

  • 相关阅读:
    vue项目根据不同环境动态配置接口
    微信内置浏览器手机按返回键,给出提示,是否要退出
    Mac更新系统后提示xcrun error
    curl请求本地域名问题
    PHP把PNG图片转化为JPG时透明背景变黑色
    安卓微信浏览器中window.location.href失效的问题
    上拉加载触底事件最简单写法
    记一次virtualbox和夜神模拟器冲突的问题
    技术总结
    input type file onchange上传文件的过程中,同一个文件二次上传无效的问题。
  • 原文地址:https://www.cnblogs.com/yyt-hehe-yyt/p/6912675.html
Copyright © 2011-2022 走看看