iPhone / Xcode:UIImage和UIImageView无法使用该程序 [英] iPhone/Xcode: UIImage and UIImageView won't get with the program

查看:126
本文介绍了iPhone / Xcode:UIImage和UIImageView无法使用该程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从iPhone照片库中挑选图像后,我试图在iPhone文档目录中保存并加载UIImage。它能够这样做,但由于某种原因,当我加载图像时,它逆时针旋转90度。这是我的saveImage和loadImage方法:

I am attempting to save and load a UIImage to and from the iPhone documents directory after the image is picked from the iPhone Photo Library. It is able to do so, but for some reason, when I load the image, it rotates it 90 degrees counterclockwise. Here is my saveImage and loadImage methods:

保存图片:

- (void)saveImage: (UIImage*)image{
if (image != nil)
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
                                                         NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString* path = [documentsDirectory stringByAppendingPathComponent: 
                      [NSString stringWithString: @"lePhoto.png"] ];
    //NSData* data = UIImagePNGRepresentation(image);
    //[data writeToFile:path atomically:YES];
    [UIImagePNGRepresentation(image) writeToFile:path atomically:YES];
    }
}

加载图片:

- (NSData*)loadImage{

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
                                                         NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* path = [documentsDirectory stringByAppendingPathComponent: 
                  [NSString stringWithString: @"lePhoto.png"] ];
NSData *data = [[NSData alloc] initWithContentsOfFile:path];


//UIImage *image = [[UIImage alloc] initWithData:data];   //my second try
//UIImage* image = [UIImage imageWithContentsOfFile:path];   //first try
//return image;
return data;
[data release];
}

我现在正在加载这样的图像,看它是否会,出于一些疯狂的原因,解决了我的问题(显然没有):

And I am now loading the image like this, to see if it would, for some crazy reason, solve my problem (it didn't, obviously):

UIImage* theImage = [[UIImage alloc] initWithData:[self loadImage]];

我做了一些测试,我有两个并排的UIImageViews,左边一个在显示之前不保存和加载图像(一旦从ImagePicker中拾取照片,它就会直接显示图像),右边的一个在保存加载发生后显示。右边的那个是唯一一个旋转90度CCW;非保存/加载图片正确显示。这只发生在实际的iPhone上。模拟器正确显示两个图像。我花了很多很多时间试图搞清楚,但无济于事。关于可能导致这种情况的任何想法?

I have done some testing where I have two UIImageViews side-by-side, one on the left that doesn't save and load the image before display (it just straight-up shows the image once a photo is picked from the ImagePicker), and one on the right that displays AFTER the save load occurs. The one on the right is the only one rotating 90 degrees CCW; the non Save/Load picture is displaying correctly. This only occurs on the actual iPhone. The simulator shows both images correctly. I have spent many, many hours attempting to figure it out, but to no avail. Any ideas as to what could be causing this?

编辑:还应该知道图像总是表示它是朝向的,即使它显然不是!它仅在所有代码运行后旋转。我已经尝试在我的代码之间进行强制重绘,以查看是否会改变一些事情,但事实并非如此(尽管有可能我甚至做错了。谁知道了。)
此外,它不会自动旋转从iPhone屏幕截图的照片。仅拍摄相机拍摄的照片。 [我还没有尝试下载照片。]这是最奇怪的事情......

It should also be known that the image always says it's oriented up, even when it obviously isn't! It only rotates after ALL of the code has been run. I've tried doing a force redraw in between my code to see if that changes things, but it doesn't (though it is possible I'm even doing that wrong. Who knows, anymore). Also, it will not autorotate photos that have been screenshot from the iPhone. Only photos that have been taken by the camera. [I haven't tried downloaded photos.] It's the weirdest thing...

如果你想知道我是如何拉动我的照片的,这就是方法这至少会说明我正在做的事情(很明显,并不是所有这些功能所需的方法,但它能让我深入了解我如何编写代码):

And if you're wondering exactly how I'm pulling my picture, here's the method that will at least shed light on what I'm doing (it's not every method needed to do this function, obviously, but it gives insight on how I've written my code):

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { 
theimageView.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; 
[self saveImage:[info objectForKey:@"UIImagePickerControllerOriginalImage"]]; 
[picker dismissModalViewControllerAnimated:YES]; 
}

非常感谢任何建议。而且我确实知道这一点:如果有办法做一些疯狂的事情来得到一些愚蠢的,从未听过的问题,很可能,我会发现它,显然:P

Any advice is greatly appreciated. And I do know this much: if there's a way to do something crazy to get some stupid, never-before-heard-of problem, chances are, I'll find it, apparently :P

推荐答案

好的。我想到了。显然,当您使用PNG表示而非JPEG表示保存时,图像方向信息不会随之保存。因此,每个加载的图像都将默认显示方向向上。所以,最简单的方法是这样做:

Ok. I figured it out. Apparently when you save things with PNG representation, and not JPEG representation, the image orientation information is not saved with it. Because of this, every image loaded will default to showing orientation up. So, the easiest way is to do this:

NSData* data = UIImageJPEGRepresentation(image,0.0);
[data writeToFile:path atomically:YES];

而不是:

[UIImagePNGRepresentation(image) writeToFile:path atomically:YES];

当然,将所有图片名称从.png更改为.jpg。上面代码的0.0部分是控制Alpha级别,这是jpegs所必需的。希望这对未来的人有所帮助!

And, of course, changing all the picture names from .png to .jpg. The 0.0 part of the code above is to control Alpha levels, which is required of jpegs. Hope this helps people in the future!

这篇关于iPhone / Xcode:UIImage和UIImageView无法使用该程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆