简体   繁体   English

iOS7 ALAsset缩略图方向错误

[英]iOS7 ALAsset Thumbnail Incorrect Orientation

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: 我正在尝试使用以下代码从iOS7中的Swift中的ALAssetsLibrary加载图像(先前从UIImagePickerController拍摄并保存在资产库中):

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. 当我使用let imageData = rep.fullResolutionImage()时,我会得到正确的图像UIImageOrientation,但是当我用let imageData = validAsset.thumbnail()替换该行时,我总是会得到.Right Orientation。 This happens only with images taken through the UIImagePickerController. 仅通过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. 为UICollectionView加载全分辨率图像非常麻烦,并且需要大量时间和内存。

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. AspectRatioThumbnail()提供了与全分辨率图像具有相同比例和方向的缩略图。

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. 像您一样,我对UIImage init使用了比例和方向参数,并获得了错误的方向,然后尝试不使用它,并且方向始终正确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM