【问题标题】:How do I get photos from a PHAsset fetch?如何从 PHAsset 提取中获取照片?
【发布时间】:2014-11-28 08:19:56
【问题描述】:

我无法通过 PHAsset 提取的照片元数据列表获取实际照片。最终我想要一系列照片。

这是我的代码(最后一分钟在返回前的最后一行转换为 UIImage):

以下是一些输出:

(lldb) po rec
<PHMoment: 0x7f8c03d62820> 12A0DFC9-5B8C-4E25-A373-122A5F1E1D8A/L0/060 (null) assetCollectionType=3/0 [2014-07-14 04:28:16 +0000 - 2014-07-14 04:28:18 +0000]

(lldb) po fetchResult
<PHFetchResult: 0x7f8c03d292c0> count=5

(lldb) po firstObject
(instance_type = Builtin.RawPointer = 0x00007f8c03d8bce0 -> 0x0000000100e83170 (void *)0x0000000100e83288: PHAsset)
 {
  instance_type = 0x00007f8c03d8bce0 -> 0x0000000100e83170 (void *)0x0000000100e83288: PHAsset
}
(lldb) po lastObject
(instance_type = Builtin.RawPointer = 0x00007f8c03d88be0 -> 0x0000000100e83170 (void *)0x0000000100e83288: PHAsset)
 {
  instance_type = 0x00007f8c03d88be0 -> 0x0000000100e83170 (void *)0x0000000100e83288: PHAsset
}
(lldb) po myPhoto
 (instance_type = Builtin.RawPointer = 0x00007f8c03d8b570 -> 0x0000000100e83170 (void *)0x0000000100e83288: PHAsset)
(lldb)

这对我没有多大好处。
我想收集实际的 UIImages。

所以我尝试进行演员表:

let myPhoto: AnyObject = fetchResult.objectAtIndex(1)! as UIImage

但这给了我一个运行时崩溃/错误:

问题:如何从我的 fetch 中收集实际的 UIImages?

【问题讨论】:

    标签: swift photolibrary


    【解决方案1】:

    方法如下:

    if url.hasPrefix("assets-library://asset") {
         let localIdentifier = url.componentsSeparatedByString("=")[1].componentsSeparatedByString("&")[0]
         let fetchResult = PHAsset.fetchAssetsWithLocalIdentifiers([localIdentifier], options: nil)
    
        if fetchResult.count > 0 {
            if let asset = fetchResult.firstObject as? PHAsset {
                PHImageManager.defaultManager().requestImageForAsset(asset, targetSize: CGSize(width: 64, height: 64), contentMode: PHImageContentMode.Default, options: nil, resultHandler: { (image:UIImage?, info:[NSObject:AnyObject]?) -> Void in
                    self.imageButton.setImage(image, forState: .Normal)
                })
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多