【发布时间】:2011-02-03 00:21:58
【问题描述】:
我从 iPhone/iPad 库中加载照片,其中大部分是纵向模式,我想知道如何在横向或纵向模式下查看照片?
【问题讨论】:
-
您想知道设备或照片的方向吗?您可以通过 Viren 的回答获得当前的设备方向。否则,比较照片的高度和宽度得到它的纵横比。但是没有办法知道实际的方向。
我从 iPhone/iPad 库中加载照片,其中大部分是纵向模式,我想知道如何在横向或纵向模式下查看照片?
【问题讨论】:
使用UIImage 实例的imageOrientation 属性。它将返回these 常量之一。
例子:
UIImage *image = // 从库中加载
if (image.imageOrientation == UIImageOrientationUp) {
NSLog(@"portrait");
} else if (image.imageOrientation == UIImageOrientationLeft || image.imageOrientation == UIImageOrientationRight) {
NSLog(@"landscape");
}
【讨论】:
NSLog(@"portrait"); 的if 块中结束,那么您将测试UIImageOrientationLeft || UIImageOrientationRight || UIImageOrientationLeftMirrored || UIImageOrientationRightMirrored,其余的将是landscape
我在运行 iOS 5.0 的 iPhone 4 上的数十张实际图片上测试了这段代码,并且能够成功地将它们全部设为纵向模式。这就是你修复/检查的方式
if (image.imageOrientation == UIImageOrientationUp ||
image.imageOrientation == UIImageOrientationDown )
{
NSLog(@"Image is in Landscape Fix it to portrait ....");
backgroundView.frame = self.view.bounds;
backgroundView.autoresizingMask=UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
backgroundView.contentMode = UIViewContentModeScaleAspectFill;
}
else
{
NSLog(@"Image is in Portrait everything is fine ...");
}
这是进行此检查的一种万无一失的方法
-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage : (UIImage *)image
editingInfo:(NSDictionary *)editingInfo
{
// Get the data for the image
NSData* imageData = UIImageJPEGRepresentation(image, 1.0);
if ([UIImage imageWithData:imageData].size.width > [UIImage imageWithData:imageData].size.height)
{
NSLog(@"Select Image is in Landscape Mode ....");
}
else
{
NSLog(@"Select Image is in Portrait Mode ...");
}
}
【讨论】: