2017-02-05 2 views
1

ich ein seltsames Verhalten erlebt habe:Lade Bild aus der Galerie durch lokale URL (Swift)

Ein imagePicker gibt einen PHAsset, und ich das Folgende und verwalten einen Imageview mit Bild aus den Daten zu präsentieren:

asset.requestContentEditingInput(with: PHContentEditingInputRequestOptions()) { (input, _) in 
    let url = input?.fullSizeImageURL 

    let imgV = UIImageView() 

    let test_url = URL(string: (url?.absoluteString)!) 

    print("><><><^^^><><>\(test_url)") 
    //prints: ><><><^^^><><>Optional(file:///var/mobile/Media/DCIM/107APPLE/IMG_7242.JPG) 
    let data = NSData(contentsOf: test_url! as URL) 
    imgV.image = UIImage(data: data! as Data) 
    imgV.backgroundColor = UIColor.cyan 

    att.imageLocalURL = url?.absoluteString//// saving the string to use in the other class 

    imgV.frame = CGRect(x: 0, y: 0, width: 100, height: 100) 

    self.view.addSubview(imgV) /// just to test that the file exists and can produce an image 

Allerdings, wenn ich die folgend in einer anderen Klasse:

if((NSURL(string: self.attachment.imageLocalURL!) as URL!).isFileURL)// checking if is Local URL 
    { 


     let test_url = URL(string: self.attachment.imageLocalURL!) // reading the value stored from before 
     print("><><><^^^><><>\(test_url)") 
     //prints :><><><^^^><><>Optional(file:///var/mobile/Media/DCIM/107APPLE/IMG_7242.JPG) 
     let data = NSData(contentsOf: test_url! as URL) 
     imageView.image = UIImage(data: data! as Data) 

    } 

die Daten sind gleich Null! Was mache ich falsch, der String für URL ist in beiden Fällen identisch!

+0

Haben Sie mit Haltepunkten versuchen und Schritt-für-Schritt sehen, wo es ein unerwartetes Ergebnis hervorbringt? – d00dle

+0

Klasse A: url_a -> string_a -> Daten -> Bild, aber Klasse B: string_a -> Daten -> Null, das ist das unerwartete Verhalten. –

Antwort

0

PHAsset Objekte sollten über die Klasse PHImageManager erreicht werden. Wenn Sie das Bild geladen werden soll synchron empfehle ich Ihnen so etwas tun:

func getImage(assetUrl: URL) -> UIImage? { 
     let asset = PHAsset.fetchAssets(withALAssetURLs: [assetUrl], options: nil) 

     guard let result = asset.firstObject else { 
      return nil 
     } 
     var assetImage: UIImage? 
     let options = PHImageRequestOptions() 
     options.isSynchronous = true 
     PHImageManager.default().requestImage(for: result, targetSize: UIScreen.main.bounds.size, contentMode: PHImageContentMode.aspectFill, options: options) { image, info in 
      assetImage = image 
     } 

     return assetImage 
    } 

Man könnte sogar eine UIImageView Erweiterung schreiben Sie das Bild direkt von einem PHAsset URL zu laden.

0

var Bilder: NSMutableArray = NSMutableArray() // die abgerufenen Bilder halten

func fetchPhotos() 
{ 
    images = NSMutableArray() 
    //totalImageCountNeeded = 3 
    self.fetchPhotoAtIndexFromEnd(0) 
} 

func fetchPhotoAtIndexFromEnd(index:Int) 
{ 
    let status : PHAuthorizationStatus = PHPhotoLibrary.authorizationStatus() 
    if status == PHAuthorizationStatus.Authorized 
    { 
    let imgManager = PHImageManager.defaultManager() 
    let requestOptions = PHImageRequestOptions() 
    requestOptions.synchronous = true 

    let fetchOptions = PHFetchOptions() 
    fetchOptions.sortDescriptors = [NSSortDescriptor(key:"creationDate", ascending: true)] 

    if let fetchResult: PHFetchResult = PHAsset.fetchAssetsWithMediaType(PHAssetMediaType.Image, options: fetchOptions) 
    { 
     if fetchResult.count > 0 
     { 
       imgManager.requestImageForAsset(fetchResult.objectAtIndex(fetchResult.count - 1 - index) as! PHAsset, targetSize: CGSizeMake(self.img_CollectionL.frame.size.height/3, self.img_CollectionL.frame.size.width/3), contentMode: PHImageContentMode.AspectFill, options: requestOptions, resultHandler: { (image, _) in 
       //self.images.addObject(image!) 
       if image != nil 
       { 
        self.images.addObject(image!) 
       } 
       if index + 1 < fetchResult.count && self.images.count < 20 //self.totalImageCountNeeded 
       { 
        self.fetchPhotoAtIndexFromEnd(index + 1) 
       } 
       else 
       { 
       } 
      }) 
      self.img_CollectionL.reloadData() 

     } 
    } 
    } 
} 
Verwandte Themen