2015-01-31 17 views
6

Fotoğraf Kitaplığı'ndan bir kullanıcının görüntü seçmesini sağlamaya çalışıyorum. Bu yüzden, UIImagePickerController'u seçiyorum. Ama burada başlangıç ​​URL'sini dosya sisteminde almayla ilgili bir sorun geliyor (CKAsset için buna ihtiyacım var).UIImage öğesinin URL'sini alma UIImagePickerController

Kodum.

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) { 
    let imageURL = info[UIImagePickerControllerReferenceURL] as NSURL 
    let path = imageURL.path! 
    let imageName = path.lastPathComponent 
    let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) 
    let documentDirectory = paths.first as String! 
    let localPath = documentDirectory + "/" + imageName 

    let imageData = NSData(contentsOfFile: localPath)! 
    let image = UIImage(data: imageData)! 

    picker.dismissViewControllerAnimated(true, completion: nil) 

} 

imageURL tür gizlenmiştir. Öğe URL'sini tanımlar, ancak Dosya Sisteminde bir değil. Ve benziyor: assets-library://asset/asset.JPG?id=B6C0A21C-07C3-493D-8B44-3BA4C9981C25&ext=JPG.

Bu mantığa göre yolasset.JPG görüntünün bir isimdir /asset.JPG olduğunu. Sonra

benim Belgeler klasörünü girip buradan yola sahip bir dosyayı bulmaya çalışıyorum:

/Users/Eugene/Library/Geliştirici/CoreSimulator/Cihazlar/3C5E9A23-8220-4B37-BD14-F1E42EEC2C7C /data/Containers/Data/Application/20EBAAE2-6C6F-4651-A48F-16A222CCB3A2/Documents/asset.JPG

Cryptic yanı fakat mevcut değil görüntü için gerçek bir yol gibi görünüyor ... resim yok bu yolla bulunabilir. Ve bu beni hasta ediyor.

Görüntüyü en baştan kaydetmem gerekiyor mu? Yoksa başka yolu var mı?

AssetsLibrary API'sini kullanarak birkaç eğitici inceledim ancak sorunumu çözmek için yararlı bir şey bulamadım. Şimdiden teşekkür ederim!

cevap

24

Tamam, sorunu çözdüm.

Yapmanız gereken tek şey sadece resmi (info[UIImagePickerControllerOriginalImage] as UIImage) ele almak ve verilen dizine kaydetmek. Sadece 1 resmi kaydetmeniz gerekiyorsa harika çalışıyor. Aşağıdaki kod kodu.

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) { 
    let imageURL = info[UIImagePickerControllerReferenceURL] as NSURL 
    let imageName = imageURL.path!.lastPathComponent 
    let documentDirectory = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first as String 
    let localPath = documentDirectory.stringByAppendingPathComponent(imageName) 

    let image = info[UIImagePickerControllerOriginalImage] as UIImage 
    let data = UIImagePNGRepresentation(image) 
    data.writeToFile(localPath, atomically: true) 

    let imageData = NSData(contentsOfFile: localPath)! 
    let photoURL = NSURL(fileURLWithPath: localPath) 
    let imageWithData = UIImage(data: imageData)! 

    picker.dismissViewControllerAnimated(true, completion: nil) 

} 
+3

Lütfen yanıtınızı Swift 2 için güncelleyebilir misiniz? Teşekkür ederim! Swift 3'te çalışmayan –

+5

kodunu derleme yapmak için dönüştürdüm, ancak döndürülen yol Swift 3 için seçilen görüntüyü – zumzum

+0

işaret etmiyor, bu çözümü kontrol edin: http://stackoverflow.com/questions/4957972/iphone-uiimagepickercontroller -save-görüntü-app-klasör/43456006 # 43456006 – XueYu

3
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) { 
//this block of code grabs the path of the file 
let imageURL = info[UIImagePickerControllerReferenceURL] as NSURL 
let imagePath = imageURL.path! 
let localPath = NSURL(fileURLWithPath: NSTemporaryDirectory()).URLByAppendingPathComponent(imagePath) 

//this block of code adds data to the above path 
let path = localPath.relativePath! 
let imageName = info[UIImagePickerControllerOriginalImage] as UIImage 
let data = UIImagePNGRepresentation(imageName) 
data?.writeToFile(imagePath, atomically: true) 

//this block grabs the NSURL so you can use it in CKASSET 
let photoURL = NSURL(fileURLWithPath: path) 
} 
+0

@Peter Kreinz, umarım bu yardımcı olur – DHuang

1

onay bu çözüm.

import AssetsLibrary 

    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) { 
      self.imagePicker = picker 
      if let selectedImage = info[UIImagePickerControllerOriginalImage] as? UIImage { 
      let controller = CropViewController(image: selectedImage) 
      controller.delegate = self 
      picker.presentViewController(controller, animated: true, completion: nil) 
      } 

     else if let imageUrl = info[UIImagePickerControllerReferenceURL] as? NSURL{ 
      let assetLibrary = ALAssetsLibrary() 
      assetLibrary.assetForURL(imageUrl , resultBlock: { (asset: ALAsset!) -> Void in 
      if let actualAsset = asset as ALAsset? { 
       let assetRep: ALAssetRepresentation = actualAsset.defaultRepresentation() 
       let iref = assetRep.fullResolutionImage().takeUnretainedValue() 
       let image = UIImage(CGImage: iref) 
       let controller = CropViewController(image: image) 
       controller.delegate = self 
       picker.presentViewController(controller, animated: true, completion: nil) 
      } 
      }, failureBlock: { (error) -> Void in 
      }) 
     } 
     } 
0

SWIFT 4u Düzgün çalışıyor, bu deneyin Can.

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


    if let imgUrl = info[UIImagePickerControllerImageURL] as? URL{ 
     let imgName = imgUrl.lastPathComponent 
     let documentDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first 
     let localPath = documentDirectory?.appending(imgName) 

     let image = info[UIImagePickerControllerOriginalImage] as! UIImage 
     let data = UIImagePNGRepresentation(image)! as NSData 
     data.write(toFile: localPath!, atomically: true) 
     //let imageData = NSData(contentsOfFile: localPath!)! 
     let photoURL = URL.init(fileURLWithPath: localPath!)//NSURL(fileURLWithPath: localPath!) 
     print(photoURL) 

    } 

    APPDEL.window?.rootViewController?.dismiss(animated: true, completion: nil) 
} 
İlgili konular