2012-01-12 27 views
15

我目前有一個應用程序使用ALAsssetsLibrary來獲取照片。我已將照片放置在圖像視圖中,並且可以上傳到服務器。在拍攝一些照片後,當我在真實設備上進行測試時,我發現應該在肖像中拍攝的照片成爲景觀。方向與ALAsset中的照片無法正確對應

因此,我叫不同的函數來獲取CGImage這樣的:

UIImage *image = [UIImage imageWithCGImage:[representation fullResolutionImage] scale:1.0 orientation:(UIImageOrientation)[representation orientation]]; 

第一個嘗試了,我用這個:

UIImage *image = [UIImage imageWithCGImage:[representation fullResolutionImage]] 

我想到了一個具有規模和方向可以給我是拍攝照片的正確方向。但它沒有給我正確的解決方案。

我是否錯過了生成照片的正確方向所需的任何內容?

+1

從設備拍攝的照片發生此問題 – Leena 2012-01-12 09:49:02

回答

44

正確的方向處理取決於您使用的iOS版本。 在iOS4和iOS 5上,縮略圖已經正確旋轉,因此您可以在不指定任何旋轉參數的情況下初始化UIImage。 但對於fullScreenImage,每個iOS版本的行爲都不相同。在iOS 5上,圖像已經在iOS 4上旋轉了。

因此,對iOS4的,你應該使用:

ALAssetRepresentation *defaultRep = [asset defaultRepresentation]; 
UIImage *_image = [UIImage imageWithCGImage:[defaultRep fullScreenImage] 
                scale:[defaultRep scale] orientation:(UIImageOrientation)[defaultRep orientation]]; 

在iOS5中下面的代碼應能正常工作:

ALAssetRepresentation *defaultRep = [asset defaultRepresentation]; 
UIImage *_image = [UIImage imageWithCGImage:[defaultRep fullScreenImage] scale:[defaultRep scale] orientation:0]; 

乾杯,

亨德里克

+0

在我的情況下,我需要fullResolutionImage,並已在iOS 6和7上進行測試。圖像不是自動旋轉的,但第一個示例代碼可以實現這個功能! 謝謝你的男人! P.S.請謹慎使用元數據將圖像保存到文件。如果你已經旋轉了圖像,那麼你必須設置EXIF方向爲0,以避免奇怪的旋轉jpeg的;) – Primoz990 2013-10-25 11:37:16

+0

我必須糾正我的評論。第一個代碼示例沒有旋轉我的FullResolution圖像,我不得不通過自己旋轉它。 但同意fullScreenImage在iOS 6和7上自動旋轉。 – Primoz990 2013-10-25 12:15:54

+0

太棒了!它也適用於iOS 7!非常感謝 ! – Vinestro 2014-02-20 17:16:02

4

試試這個代碼: -

UIImage* img = [UIImage imageWithCGImage:asset.thumbnail]; 
img = [UIImage imageWithCGImage:img.CGImage scale:1.0 orientation:UIImageOrientationUp]; 

這可能會幫助你。

+0

我現在無法嘗試,因爲我沒有自己的設備。你介意給我解釋這兩行代碼嗎?第一行的img是資產的縮略圖,但不是實際的圖像。 – LittleFunny 2012-01-12 10:05:53

+0

我已經使用了圖像的縮略圖,並且我保持圖像的方向向上,所以它不會旋轉它在我的情況下工作。 – Leena 2012-01-12 10:20:25

+0

也許我有一個不同的問題...但是,感謝您的幫助 – LittleFunny 2012-01-12 10:45:21

3

我的經驗是有限的,以IOS 5.x,但我可以告訴你,縮略圖和全屏圖像是專業的佩爾利。這是垂直拍攝時水平方向的全分辨率圖像。我的解決方案是使用上的UIImage的類別,我從這裏得到:

http://www.catamount.com/forums/viewtopic.php?f=21&t=967&start=0

它提供了一個很好的方法,旋轉在這樣一個UIImage:

 UIImage *tmp = [UIImage imageWithCGImage:startingFullResolutionImage]; 
     startingFullResolutionImage = [[tmp imageRotatedByDegrees:-90.0f] CGImage]; 
+0

我只是使用fullresolutionimage,它爲我工作。謝謝。 – Ali 2012-07-23 14:08:34

0

對於fullResolutionImage,我想提供瞭解決方案如下,

ALAssetRepresentation *rep = [asset defaultRepresentation]; 

// First, write orientation to UIImage, i.e., EXIF message. 
UIImage *image = [UIImage imageWithCGImage:[rep fullResolutionImage] scale:rep.scale orientation:(UIImageOrientation)rep.orientation]; 
// Second, fix orientation, and drop out EXIF 
if (image.imageOrientation != UIImageOrientationUp) { 
    UIGraphicsBeginImageContextWithOptions(image.size, NO, image.scale); 
    [image drawInRect:(CGRect){0, 0, image.size}]; 
    UIImage *normalizedImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    image = normalizedImage; 
} 
// Third, compression 
NSData *imageData = UIImageJPEGRepresentation(image, 1.0); 

imageData是你想要的,只是把它上傳到你的照片的服務器。

順便說一下,如果您認爲EXIF有用,您可以根據需要將其補充到normalizedImage

相關問題