I'm trying to load images (taken earlier from the UIImagePickerController and saved in the Assets Library) from the ALAssetsLibrary in iOS7 in Swift using the code below:
assetsLibrary.assetForURL(NSURL(string: image.diskPath), resultBlock: {
asset in
if let validAsset = asset {
let rep = validAsset.defaultRepresentation()
let imageData = rep.fullResolutionImage()
//let imageData = validAsset.thumbnail()
var imageOrientation: UIImageOrientation = .Down
let orientValueFromImage = validAsset.valueForProperty("ALAssetPropertyOrientation") as NSNumber
imageOrientation = UIImageOrientation.fromRaw(orientValueFromImage.integerValue)!
let imageToAdd = UIImage(CGImage: imageData.takeUnretainedValue(), scale: 1, orientation: imageOrientation)
self.imagesForCollectionView.append(imageToAdd)
fetchedImageCount++
if fetchedImageCount == fetchedImages.count{
self.collectionView.reloadData()
}
}
}, failureBlock: {error in
NSLog("error %@", error.debugDescription)
})
When I use let imageData = rep.fullResolutionImage(), I get the correct UIImageOrientation for the image, but when I replace that line with let imageData = validAsset.thumbnail(), I always get a .Right Orientation. This happens only with images taken through the UIImagePickerController. Any Ideas on why this is happening and how to fix it? Loading a full resolution image for a UICollectionView is a bummer and takes a lot of time and memory.
How can I get the correct orientation of the thumbnail? Thanks.
Have you tried something like :
UIImage(CGImage:asset?.aspectRatioThumbnail().takeUnretainedValue())
aspectRatioThumbnail() gives you a thumbnail that have the same ratio and orientation as the full resolution image.
Like you I used scale and orientation parameters for UIImage init and got wrong orientation, I then tried without and the orientation is always correct.