3

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
4

2 に答える 2

8

コードのアセット指向部分では、代わりに

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

試す

ALAssetOrientation orientation = [[asset valueForProperty:@"ALAssetPropertyOrientation"] intValue];
于 2011-06-23T01:36:15.623 に答える
0

画像を画像コンテキストに再描画できます。次に、コンテキストから画像データを取得し、必要に応じて、データを送信するか、後で送信するためにファイルを保存します。

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

ポートレート写真の回転方法によっては、「正しい」画像の向きが必要になる場合がありますが、それは 1 つのテストを行うだけの問題です。

于 2011-06-10T22:09:35.227 に答える