我刚开始学习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
答案 0 :(得分:8)
对于代码的资产定位部分,而不是
ALAssetRepresentation *rep = [myasset defaultRepresentation];
ALAssetOrientation orientation = [rep orientation];
尝试
ALAssetOrientation orientation = [[asset valueForProperty:@"ALAssetPropertyOrientation"] intValue];
答案 1 :(得分: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();
您可能需要“正确”的图像方向,具体取决于您的肖像照片的旋转方式,但这只是进行一次测试的问题。