带有URL

时间:2016-03-30 10:47:31

标签: ios swift sdwebimage

我们可以使用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)

}

2 个答案:

答案 0 :(得分:0)

注意UIImagePickerController委托 imagePickerController:didFinishPickingMediaWithInfo

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

  

包含原始图像和编辑图像的字典(如果是)   图像被选中;或电影的文件系统URL,如果是电影的话   采摘。该词典还包含任何相关的编辑信息。   编辑信息键中列出了此词典的键。

所以,如果它是你正在看的图像,那么它就会出现在字典中。

您可以直接获取原始图像 -

let image = info[UIImagePickerControllerOriginalImage]

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

答案 1 :(得分:0)

此处infoNSDictonary并获取该密钥为UIImagePickerControllerOriginalImage

请参阅此代码:

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

    self.dismissModalViewControllerAnimated(true)

}