IOS Swift 使用 URL 加载(本地)图像

IOS Swift load (local)image with URL

我们可以使用 URL 选项使用 SDWebimage 加载在设备上本地找到的图像吗?如果是,那么我正在尝试使用 SDWebimage 来显示图像,下面是我的代码,我无法在 imageview 中加载图像。它仍然是空白的。请帮助我了解我做错了什么?

func addImage(sender: AnyObject){

    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.SavedPhotosAlbum){

        picController.sourceType = UIImagePickerControllerSourceType.SavedPhotosAlbum;
        picController.allowsEditing = true
        self.presentViewController(picController, animated: true, completion: nil)
    }
}

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
    let url = info[UIImagePickerControllerReferenceURL] as! NSURL
    titleImageView.sd_setImageWithURL(url)

    self.dismissViewControllerAnimated(true, completion: nil)

}

注意 UIImagePickerController 委托 imagePickerController: didFinishPickingMediaWithInfo

将具有文档中描述的信息-

A dictionary containing the original image and the edited image, if an image was picked; or a filesystem URL for the movie, if a movie was picked. The dictionary also contains any relevant editing information. The keys for this dictionary are listed in Editing Information Keys.

因此,如果您正在查看的是图像,那么它就在字典中。

可以直接获取原图-

let image = info[UIImagePickerControllerOriginalImage]

if let image = image {
   //Set this image to your image view directly
   titleImageView.image = image
}

这里 info 作为 NSDictonary 得到那个密钥是 UIImagePickerControllerOriginalImage

查看此代码:

func imagePickerController(picker: UIImagePickerController!, didFinishPickingMediaWithInfo info:NSDictionary!) {
    var tempImage:UIImage = info[UIImagePickerControllerOriginalImage] as UIImage
    imagePreview.image  = tempImage

    self.dismissModalViewControllerAnimated(true)

}