【问题标题】:Fetching last photo taken with Swift获取使用 Swift 拍摄的最后一张照片
【发布时间】:2016-07-18 08:55:10
【问题描述】:

首先我会说我是 swift 新手,以及堆栈溢出,所以如果我搞砸了,我深表歉意。

我正在尝试获取使用 Swift 拍摄的最后一张照片。我发现似乎应该可以工作的代码。 http://blog.swilliams.me/words/2015/08/09/finding-the-last-photo-taken/

import UIKit
import Photos

typealias ImageCallback = (UIImage? -> Void)

func fetchLastPhoto(resizeTo size: CGSize?, imageCallback: ImageCallback) {
let fetchOptions = PHFetchOptions()
fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
fetchOptions.fetchLimit = 1

if let fetchResult = PHAsset.fetchAssetsWithMediaType(.Image, options: fetchOptions) {
    if let asset = fetchResult.firstObject as? PHAsset {
        let manager = PHImageManager.defaultManager()
        let targetSize = size == nil ? CGSize(width: asset.pixelWidth, height: asset.pixelHeight) : size!
        manager.requestImageForAsset(asset,
            targetSize: targetSize,
            contentMode: .AspectFit,
            options: nil,
            resultHandler: { image, info in
            imageCallback(image)
        })
    } else {
        imageCallback(nil)
    }
  }
}

我不断收到以“if let fetchResult = PHAsset.fet....”开头的行的错误

错误说,“

条件绑定的初始化器必须是 Optional 类型,而不是 'PHFetchResult'

当我选择单击“fetchResult”时,它不会告诉我它是什么类型的值并给出错误,所以我知道它没有正确设置。

我觉得解决方案可能非常简单,我还没有尝试过。

有人知道是什么导致了这个错误吗?

【问题讨论】:

    标签: swift phasset photosframework


    【解决方案1】:

    检查PHAsset.fetchAssetsWithMediaType(_:options:) 方法的声明。 (快速帮助就足够了。)

    class func fetchAssetsWithMediaType(mediaType: PHAssetMediaType, options: PHFetchOptions?) -> PHFetchResult
    

    该方法的返回类型是PHFetchResult,它没有尾随?,意味着它不是可选的。

    您不能将非可选类型用作可选绑定的右侧,这就是错误消息的含义。这也意味着您无需使用if-let检查可空性

    只需删除导致错误的if-let 并将其替换为简单的let 声明即可。

    func fetchLastPhoto(resizeTo size: CGSize?, imageCallback: ImageCallback) {
        let fetchOptions = PHFetchOptions()
        fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
        fetchOptions.fetchLimit = 1
    
        let fetchResult = PHAsset.fetchAssetsWithMediaType(.Image, options: fetchOptions)
        //`fetchResult` is not nil here (without using `if-let`)
        if let asset = fetchResult.firstObject as? PHAsset {
            let manager = PHImageManager.defaultManager()
            let targetSize = size == nil ? CGSize(width: asset.pixelWidth, height: asset.pixelHeight) : size!
            manager.requestImageForAsset(asset,
                                         targetSize: targetSize,
                                         contentMode: .AspectFit,
                                         options: nil,
                                         resultHandler: { image, info in
                                            imageCallback(image)
            })
        } else {
            imageCallback(nil)
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2013-01-14
      • 2015-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多