从 ALAssset 检索时的 CGImage 方向问题

发布于 11-15 01:53 字数 1043 浏览 4 评论 0原文

我刚刚开始学习 iPhone 应用程序开发。我正在尝试使用 ALAsset 类从我的相册中检索照片并将照片上传到我的服务器。然而,在人像模式下拍摄的照片向左旋转90度,而风景照片则正确上传。我做错了什么?在 NSLog 中,我确实看到方向为 3,但照片仍然向左旋转 90 度。任何帮助将不胜感激。

这是我的代码片段:

ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
{
   NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
   NSString *documentsDirectory = [paths objectAtIndex:0];

   imagePath = [[documentsDirectory stringByAppendingPathComponent:@"latest_photo.jpg"] copy];
   NSLog(@"imagePath = %@", imagePath);

   ALAssetRepresentation *rep = [myasset defaultRepresentation];
   ALAssetOrientation orientation = [rep orientation];
   NSLog(@"Orientation = %d", orientation);

   CGImageRef iref = [rep fullResolutionImage];

   if (iref) {
      UIImage *largeimage = [UIImage imageWithCGImage:iref scale:1.0 orientation:orientation];
      NSData *webData = UIImageJPEGRepresentation(largeimage,0.5);

      [webData writeToFile:imagePath atomically:YES];

      // Upload the image

I am just starting to learn iPhone app development. I am trying to retrieve photos from my photo album using ALAsset class and upload the photos to my server. However, the photos that were taken in the Portrait mode are rotated 90 degrees to the left while the landscape photos are properly uploaded. What am I doing wrong? In the NSLog, I do see the orientation to be 3, but still the photo is rotated to 90 degrees to the left. Any help will be greatly appreciated.

Here's the snippet of my code:

ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
{
   NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
   NSString *documentsDirectory = [paths objectAtIndex:0];

   imagePath = [[documentsDirectory stringByAppendingPathComponent:@"latest_photo.jpg"] copy];
   NSLog(@"imagePath = %@", imagePath);

   ALAssetRepresentation *rep = [myasset defaultRepresentation];
   ALAssetOrientation orientation = [rep orientation];
   NSLog(@"Orientation = %d", orientation);

   CGImageRef iref = [rep fullResolutionImage];

   if (iref) {
      UIImage *largeimage = [UIImage imageWithCGImage:iref scale:1.0 orientation:orientation];
      NSData *webData = UIImageJPEGRepresentation(largeimage,0.5);

      [webData writeToFile:imagePath atomically:YES];

      // Upload the image

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

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

发布评论

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

评论(2

把人绕傻吧 2024-11-22 01:53:18

对于代码的资产导向部分,而不是

ALAssetRepresentation *rep = [myasset defaultRepresentation];
ALAssetOrientation orientation = [rep orientation];

Try

ALAssetOrientation orientation = [[asset valueForProperty:@"ALAssetPropertyOrientation"] intValue];

For the asset orientation part of your code, instead of

ALAssetRepresentation *rep = [myasset defaultRepresentation];
ALAssetOrientation orientation = [rep orientation];

Try

ALAssetOrientation orientation = [[asset valueForProperty:@"ALAssetPropertyOrientation"] intValue];
音盲 2024-11-22 01:53:18

您可以将图像重新绘制到图像上下文中。然后从上下文中获取图像数据,并根据您的需要发送数据或存储文件以供稍后发送。

UIGraphicsBeginImageContext(largeImage.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextRotateCTM(context, M_PI / 2); // Or negative M_PI depending on the image's orientation
largeImage.imageOrientation = UIImageOrientationLeft;
[largeImage drawAtPoint:CGPointZero];
NSData *rotatedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

您可能需要“正确”的图像方向,具体取决于您的肖像照片的旋转方式,但这只是进行一个测试的问题。

You can redraw the image into an image context. Then get the image data from the context, and depending on your needs, send the data or store the file to send later.

UIGraphicsBeginImageContext(largeImage.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextRotateCTM(context, M_PI / 2); // Or negative M_PI depending on the image's orientation
largeImage.imageOrientation = UIImageOrientationLeft;
[largeImage drawAtPoint:CGPointZero];
NSData *rotatedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

You might need the 'right' image orientation, depending on which way your portrait photos are rotated, but its just a matter of making one test.

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