|
| 1 | +// |
| 2 | +// PHFetchResultExtension.swift |
| 3 | +// SDEAlbumTransition |
| 4 | +// |
| 5 | +// Created by seedante on 15/10/15. |
| 6 | +// Copyright © 2015年 seedante. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import Photos |
| 10 | + |
| 11 | +extension PHFetchResult{ |
| 12 | + class func fetchPosterImageForAssetCollection(assetCollection: PHAssetCollection, targetSize: CGSize) -> UIImage?{ |
| 13 | + |
| 14 | + let fetchOptions = PHFetchOptions() |
| 15 | + fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)] |
| 16 | + let fetchResult = PHAsset.fetchAssetsInAssetCollection(assetCollection, options: fetchOptions) |
| 17 | + |
| 18 | + var posterImage: UIImage? |
| 19 | + fetchResult.enumerateObjectsAtIndexes(NSIndexSet(index: 0), options: NSEnumerationOptions.Concurrent){ |
| 20 | + (assetItem: AnyObject!, index: Int, stop: UnsafeMutablePointer<ObjCBool>) -> Void in |
| 21 | + |
| 22 | + let requestOptions = PHImageRequestOptions() |
| 23 | + requestOptions.synchronous = true |
| 24 | + PHImageManager.defaultManager().requestImageForAsset(assetItem as! PHAsset, targetSize: targetSize, contentMode: .AspectFill, options: requestOptions, resultHandler:{ |
| 25 | + (image: UIImage?, info: [NSObject: AnyObject]?) -> Void in |
| 26 | + posterImage = image |
| 27 | + }) |
| 28 | + } |
| 29 | + |
| 30 | + return posterImage |
| 31 | + } |
| 32 | + |
| 33 | + class func fetchPosterImageForAssetCollection(assetCollection: PHAssetCollection, imageView: UIImageView, targetSize: CGSize){ |
| 34 | + let fetchOptions = PHFetchOptions() |
| 35 | + fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)] |
| 36 | + let fetchResult = PHAsset.fetchAssetsInAssetCollection(assetCollection, options: fetchOptions) |
| 37 | + |
| 38 | + fetchResult.enumerateObjectsAtIndexes(NSIndexSet(index: 0), options: NSEnumerationOptions.Concurrent){ |
| 39 | + (assetItem: AnyObject!, index: Int, stop: UnsafeMutablePointer<ObjCBool>) -> Void in |
| 40 | + |
| 41 | + PHImageManager.defaultManager().requestImageForAsset(assetItem as! PHAsset, targetSize: targetSize, contentMode: .AspectFill, options: nil, resultHandler:{ |
| 42 | + (image, info) -> Void in |
| 43 | + imageView.image = image |
| 44 | + }) |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + func fetchImageAtIndex(index: Int, targetSize: CGSize) -> UIImage?{ |
| 49 | + if self.count > index{ |
| 50 | + var fetchedImage: UIImage? |
| 51 | + self.enumerateObjectsAtIndexes(NSIndexSet(index: index), options: NSEnumerationOptions.Concurrent){ |
| 52 | + (assetItem, index, stop) -> Void in |
| 53 | + |
| 54 | + if let asset = assetItem as? PHAsset{ |
| 55 | + let requestOptions = PHImageRequestOptions() |
| 56 | + requestOptions.synchronous = true |
| 57 | + |
| 58 | + PHImageManager.defaultManager().requestImageForAsset(asset, targetSize: targetSize, contentMode: .AspectFill, options: requestOptions, resultHandler: { |
| 59 | + (image, info) in |
| 60 | + fetchedImage = image |
| 61 | + }) |
| 62 | + } |
| 63 | + |
| 64 | + } |
| 65 | + return fetchedImage |
| 66 | + } |
| 67 | + |
| 68 | + return nil |
| 69 | + } |
| 70 | + |
| 71 | + func fetchImageAtIndex(index: Int, imageView:UIImageView, targetSize: CGSize){ |
| 72 | + if self.count > index{ |
| 73 | + self.enumerateObjectsAtIndexes(NSIndexSet(index: index), options: NSEnumerationOptions.Concurrent){ |
| 74 | + (assetItem, index, stop) -> Void in |
| 75 | + |
| 76 | + if let asset = assetItem as? PHAsset{ |
| 77 | + PHImageManager.defaultManager().requestImageForAsset(asset, targetSize: targetSize, contentMode: .AspectFill, options: nil, resultHandler: { |
| 78 | + (image, info) in |
| 79 | + imageView.image = image |
| 80 | + }) |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + func getAssetAtIndex(index: Int) -> PHAsset?{ |
| 87 | + if self.count > index{ |
| 88 | + var asset: PHAsset? |
| 89 | + self.enumerateObjectsAtIndexes(NSIndexSet(index: index), options: NSEnumerationOptions.Concurrent, usingBlock: { |
| 90 | + (assetItem, _, _) in |
| 91 | + |
| 92 | + asset = assetItem as? PHAsset |
| 93 | + }) |
| 94 | + |
| 95 | + return asset |
| 96 | + } |
| 97 | + |
| 98 | + return nil |
| 99 | + } |
| 100 | +} |
0 commit comments