我为用户提供了从他们的相机胶卷中挑选照片并上传到服务器的能力。
方向在相机胶卷中显示正确。
当我使用模拟器(iphone5,iphone6等)时,一切正常,并且上传到服务器时照片的方向正确。
当我连接设备并选择照片时,图像始终朝向90 CCW。如果我在保存结果之前注销image.imageOrientation,我可以看到它的'3'表示UIImageOrientationRight。
有谁知道为什么在设备上其90 CCW和模拟器正确?
这是我的代码:
(event.eventAttachments是PHAssets的数组)
__block NSMutableArray *images = [[NSMutableArray alloc] init];
PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
options.networkAccessAllowed = YES;
options.synchronous = YES;
for(id asset in event.eventAttachments) {
CGFloat scale = [UIScreen mainScreen].scale;
CGSize targetSize = CGSizeMake(CGRectGetWidth([UIScreen mainScreen].bounds) * scale, CGRectGetHeight([UIScreen mainScreen].bounds) * scale);
[[PHImageManager defaultManager] requestImageForAsset:asset
targetSize:targetSize
contentMode:PHImageContentModeAspectFit
options:options
resultHandler:^(UIImage *result, NSDictionary *info) {
if (result) {
NSMutableDictionary *imageDict = [[NSMutableDictionary alloc] init];
[imageDict setObject:result forKey:@"image"];
[imageDict setObject:[NSString stringWithFormat:@"image.jpg"] forKey:@"name"];
[images addObject: imageDict];
}
}];
}
Run Code Online (Sandbox Code Playgroud)
我NSLog image.UIImageOrientation我看到3。
编辑:要补充一点,如果我使用我的设备,然后转到google图片并下载随机图片,然后选择该图片的方向是正确的。
因此,方向仅适用于使用设备拍摄的照片。因为模拟器是我在使用模拟器附带的“原始”图像(因为我无法使用模拟器拍照)。
我首先通过运行图像解决了我的问题:
- (UIImage *)normalizedImage: (UIImage *)image {
if (image.imageOrientation == UIImageOrientationUp) return image;
UIGraphicsBeginImageContextWithOptions(image.size, NO, image.scale);
[image drawInRect:(CGRect){0, 0, image.size}];
UIImage *normalizedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return normalizedImage;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1391 次 |
| 最近记录: |