2012-06-04 19 views
4

我正在嘗試使用fullScreenImage方法從ALAssetRepresentation正確旋轉UIImage。我有幾張測試照片以各種設備方向拍攝;照片在照片應用中正確顯示。對於fullScreenImage文檔說:如何從ALAssetRepresentation獲取正確旋轉的UIImage?

在iOS 5中,後來,這個方法返回一個完全裁剪,旋轉,調整 圖像完全一樣,用戶會在照片或圖像 選擇器看到的。

要從CGImage創建正確地旋轉的UIImage對象,可以使用 imageWithCGImage:scale:orientation:initWithCGImage:scale:orientation:,傳球orientationscale的值。

鑑於文檔,我的代碼看起來是這樣的:

ALAssetRepresentation *rep = [asset defaultRepresentation]; 
UIImage *img = [UIImage 
    imageWithCGImage:[rep fullScreenImage] 
    scale:[rep scale] 
    orientation:[rep orientation]]; 

但由此產生的UIImage的自轉是錯誤的。當我將[rep orientation]替換爲UIImageOrientationUp時,對於所有測試照片來說圖像都很好。顯然,我很猶豫要堅持這個「解決方案」,因爲它感覺像一個黑客。我究竟做錯了什麼?

回答

11
ALAssetRepresentation *rep = [asset defaultRepresentation]; 
UIImage *img = [UIImage 
    imageWithCGImage:[rep fullScreenImage] 
    scale:[rep scale] 
    orientation:UIImageOrientationUp]; 

正確的是在iOS 5下fullscreenimages已經旋轉(所以它總是「向上」)。在iOS 4下,行爲是不同的。有關更深入的解釋,請參見Orientation does not behave correctly with Photo in ALAsset

+0

你好,亨德里克,並再次感謝您的答案!然後我會提交一個針對文檔的bug請求,因爲它沒有明確說明第二段只適用於iOS4。 PS:你可能想從你的帖子中刪除簽名,看看[這個元線程](http://meta.stackexchange.com/questions/5029/are-taglines-signatures-disallowed)。 – zoul

+0

作爲[rdar:// 11604456](http://openradar.appspot.com/11604456)提交,並在即將推出的iOS 6發行版的一個月後通過忽略第二個稍有混淆的段落解決。 – zoul

+0

謝謝!我現在正在爲這個問題(和其他相關問題)苦苦掙扎近一個星期。這是iOS5/iOS6真正意義上的第一個答案。你是我的英雄;) –