iPhone/Xcode:UIImage 和 UIImageView 无法使用该程序

发布于 2024-10-16 19:47:10 字数 2468 浏览 1 评论 0原文

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

保存图像:

- (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];
}

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

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

我已经完成了一些测试,我有两个并排的 UIImageView,一个在左侧,在显示前不会保存和加载图像(一旦从 ImagePicker 中选取照片,它就会直接显示图像),另一个在左侧保存加载发生后显示的右侧。右边的是唯一一个逆时针旋转90度的;非保存/加载图片显示正确。这只发生在实际的 iPhone 上。模拟器正确显示两个图像。我花了很多很多时间试图弄清楚,但无济于事。关于可能导致此问题的任何想法?

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

如果你想知道我到底是如何拉我的照片的,这里的方法至少可以阐明我正在做的事情(这不是显然,每个方法都需要执行此功能,但它提供了我如何编写代码的见解):

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

非常感谢任何建议。我确实知道这一点:如果有一种方法可以做一些疯狂的事情来解决一些愚蠢的、从未听说过的问题,那么很可能,我会找到它,显然:P

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:

Save Image:

- (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];
    }
}

Load Image:

- (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]];

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?

EDIT: 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]; 
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清风夜微凉 2024-10-23 19:47:10

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

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

而不是这样:

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

当然,将所有图片名称从 .png 更改为 .jpg。上面代码的0.0部分是控制Alpha级别,这是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];

instead of this:

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

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!

喜你已久 2024-10-23 19:47:10

我假设您正在使用资产库框架来获取图像。如果是这样,您将需要获取结果块中图像的方向,并操作 UIImage 进行调整。我有类似以下内容:

ALAssetsLibraryAssetForURLResultBlock resultBlock = ^(ALAsset* asset)
{
  ALAssetRepresentation* rep = [asset defaultRepresentation];
  _orientation = rep.orientation;

...

}

旁白:多棒的类型名称啊,嗯?天哪,Objective-C 需要命名空间吗!

I assume you're using the Assets Library Framework to get the image. If so, you'll want to get the orientation of the image in your results block, and manipulate the UIImage to adjust. I have something like the following:

ALAssetsLibraryAssetForURLResultBlock resultBlock = ^(ALAsset* asset)
{
  ALAssetRepresentation* rep = [asset defaultRepresentation];
  _orientation = rep.orientation;

...

}

Aside: What a great type name, huh? Holy moly, does Objective-C need namespaces!

落花随流水 2024-10-23 19:47:10

非常感谢...
这段代码很有效,并为我节省了几个小时的工作时间。

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

代替

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

Thanks so much...
This code has worked and saved several hours of work for me.

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

in the place of

[UIImagePNGRepresentation(image) writeToFile:path atomically:YES];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文