gpt4 book ai didi

iphone - 将图像从UIImagePickerController存储在iPhone中?

转载 作者:行者123 更新时间:2023-12-01 19:15:37 25 4
gpt4 key购买 nike

我试图将NSUserDefault中的图像(UIImagePickerController)存储在iPhone中。

这种存储图像的方式:

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *originalImage=[info objectForKey:UIImagePickerControllerOriginalImage];
NSData * imageData = UIImagePNGRepresentation(originalImage);
NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
[user setValue:imageData forKey:@"pictureData"];
}

这种检索图像的方式:
NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
UIImage *myImage = [UIImage imageWithData:[user valueForKey:@"pictureData"]];
[btn2 setImage:myImage forState:UIControlStateNormal];

当我从 NSUserDefaults检索图像以设置 UIButton时,最终显示的是图像,但是方向从下到右变化,如何解决此问题?请帮我

最佳答案

UIImage具有imageOrientation属性,该属性指示UIImageView和其他UIImage使用者旋转原始图像数据。此标志很有可能会保存到上载的jpeg图像中的exif数据中,但是用于查看它的程序并不遵守该标志。

要旋转UIImage以使其在上载时正确显示,可以使用如下类别:

UIImage + fixOrientation.h

@interface UIImage (fixOrientation)

- (UIImage *)fixOrientation;

@end

UIImage + fixOrientation.m
@implementation UIImage (fixOrientation)

- (UIImage *)fixOrientation {

// No-op if the orientation is already correct
if (self.imageOrientation == UIImageOrientationUp) return self;

// We need to calculate the proper transformation to make the image upright.
// We do it in 2 steps: Rotate if Left/Right/Down, and then flip if Mirrored.
CGAffineTransform transform = CGAffineTransformIdentity;

switch (self.imageOrientation) {
case UIImageOrientationDown:
case UIImageOrientationDownMirrored:
transform = CGAffineTransformTranslate(transform, self.size.width, self.size.height);
transform = CGAffineTransformRotate(transform, M_PI);
break;

case UIImageOrientationLeft:
case UIImageOrientationLeftMirrored:
transform = CGAffineTransformTranslate(transform, self.size.width, 0);
transform = CGAffineTransformRotate(transform, M_PI_2);
break;

case UIImageOrientationRight:
case UIImageOrientationRightMirrored:
transform = CGAffineTransformTranslate(transform, 0, self.size.height);
transform = CGAffineTransformRotate(transform, -M_PI_2);
break;
case UIImageOrientationUp:
case UIImageOrientationUpMirrored:
break;
}

switch (self.imageOrientation) {
case UIImageOrientationUpMirrored:
case UIImageOrientationDownMirrored:
transform = CGAffineTransformTranslate(transform, self.size.width, 0);
transform = CGAffineTransformScale(transform, -1, 1);
break;

case UIImageOrientationLeftMirrored:
case UIImageOrientationRightMirrored:
transform = CGAffineTransformTranslate(transform, self.size.height, 0);
transform = CGAffineTransformScale(transform, -1, 1);
break;
case UIImageOrientationUp:
case UIImageOrientationDown:
case UIImageOrientationLeft:
case UIImageOrientationRight:
break;
}

// Now we draw the underlying CGImage into a new context, applying the transform
// calculated above.
CGContextRef ctx = CGBitmapContextCreate(NULL, self.size.width, self.size.height,
CGImageGetBitsPerComponent(self.CGImage), 0,
CGImageGetColorSpace(self.CGImage),
CGImageGetBitmapInfo(self.CGImage));
CGContextConcatCTM(ctx, transform);
switch (self.imageOrientation) {
case UIImageOrientationLeft:
case UIImageOrientationLeftMirrored:
case UIImageOrientationRight:
case UIImageOrientationRightMirrored:
// Grr...
CGContextDrawImage(ctx, CGRectMake(0,0,self.size.height,self.size.width), self.CGImage);
break;

default:
CGContextDrawImage(ctx, CGRectMake(0,0,self.size.width,self.size.height), self.CGImage);
break;
}

// And now we just create a new UIImage from the drawing context
CGImageRef cgimg = CGBitmapContextCreateImage(ctx);
UIImage *img = [UIImage imageWithCGImage:cgimg];
CGContextRelease(ctx);
CGImageRelease(cgimg);
return img;
}

@end

关于iphone - 将图像从UIImagePickerController存储在iPhone中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13582300/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com