Swift 3从本地目录

时间:2017-03-27 12:43:30

标签: ios swift3

我是iOS开发人员的新手,无法弄清楚如何从本地目录加载imageView中的图像。 我使用imagePickerController来选择图像,获取其信息,然后使用此信息在imageView中显示图像。

以下是选择图片及其信息词典的代码:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        let image             = info[UIImagePickerControllerOriginalImage] as! UIImage

        let imageUrl          = info[UIImagePickerControllerReferenceURL] as! NSURL
        let imageName         = imageUrl.lastPathComponent
        let documentDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
        let photoURL          = NSURL(fileURLWithPath: documentDirectory)
        let localPath         = photoURL.appendingPathComponent(imageName!)!

        imagesList.append(localPath)

        picker.dismiss(animated: true, completion: nil)
    }

然后,这里是我正在寻找帮助的地方,我想使用localPath或imageUrl将图像加载到另一个imageView中,但无法弄清楚如何。 我试过了

func changeImage(_ sender: Any) {
        if imagesList.count > 0 {
            let imageUrlPath = imagesList[indexList]
            let urlString: String = imageUrlPath.absoluteString

            imageView.image = UIImage(contentsOfFile: urlString)

            indexList = (indexList + 1) % imagesList.count
        }
    }

但我无法有所作为。 知道如何实现这个目标吗?

感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

您希望保存对PHAsset对象的引用,而不是URL字符串。

首先将数组定义为:

var imagesList = [PHAsset]()

然后,在didFinishPickingMediaWithInfo

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

    dismiss(animated: true)

    // get the selected image as a UIImage object
    let image = info[UIImagePickerControllerOriginalImage] as! UIImage
    imageView.image = image

    // save a PHAsset reference in imagesList array for later use
    if let imageUrl = info[UIImagePickerControllerReferenceURL] as? URL{

        let assets = PHAsset.fetchAssets(withALAssetURLs: [imageUrl], options: nil)

        if let p = assets.firstObject {

            imagesList.append(p)

        }

    }

}

接下来,添加一对"方便"功能:

func getAssetFullsize(asset: PHAsset) -> UIImage {
    let manager = PHImageManager.default()
    let option = PHImageRequestOptions()
    var img = UIImage()
    option.isSynchronous = true
    let w = asset.pixelWidth
    let h = asset.pixelHeight
    manager.requestImage(for: asset, targetSize: CGSize(width: w, height: h), contentMode: .aspectFit, options: option, resultHandler: {(result, info)->Void in
        img = result!
    })
    return img
}

func getAssetThumbnail(asset: PHAsset) -> UIImage {
    let manager = PHImageManager.default()
    let option = PHImageRequestOptions()
    var thumbnail = UIImage()
    option.isSynchronous = true
    let w = 100
    let h = 100
    manager.requestImage(for: asset, targetSize: CGSize(width: w, height: h), contentMode: .aspectFit, options: option, resultHandler: {(result, info)->Void in
        thumbnail = result!
    })
    return thumbnail
}

最后,当你想要获得实际图像时:

func changeImage(_ sender: Any) {
    if imagesList.count > 0 {

        // get the full-size image from PHAsset
        imageView.image = getAssetFullsize(asset: imagesList[indexList])

        // or, just get a thumbnail-sized image
        //imageView.image = getAssetThumbnail(asset: imagesList[indexList])

        indexList = (indexList + 1) % imagesList.count

    }
}

当然,您希望添加适当的错误检查,自己的大小调整,命名,跟踪等等......但我认为这是您想要的方向。