0

Ich habe mehrere Möglichkeiten ausprobiert, um den Speicherort eines Bilds zu ermitteln, das die UIImagePickerController Camera übernommen hat.Bildspeicherort von UIImagePickerController abrufen

Was ich erreichen möchte, ist, dass, ich möchte ein Bild aus, mit UIImagePickerControllerCamera und ich habe es in Photo Library zu speichern, so dass nur ich kann das PHAsset von ihm und auch den Standort verknüpft zurücknehmen.

//MARK: Saving an Image to PhotoLibrary and taking back the PHAsset 

    class func savingThis(image : UIImage, completion : (asset : PHAsset?) ->()) 
    { 
     var localIdentifier : String? 
     let imageManager = PHPhotoLibrary.sharedPhotoLibrary() 

     imageManager.performChanges({() -> Void in 

      let request = PHAssetChangeRequest.creationRequestForAssetFromImage(image) 

      if let properAsset = request.placeholderForCreatedAsset { 
       localIdentifier = properAsset.localIdentifier 
      } 
      else { 
       completion(asset: nil) 
      } 

      }, completionHandler: { (success, error) -> Void in 

       if let properLocalIdentifier = localIdentifier { 

        let result = PHAsset.fetchAssetsWithLocalIdentifiers([properLocalIdentifier], options: nil) 
        if result.count > 0 { 
         completion(asset: result[0] as? PHAsset) 
        } 
        else { 
         completion(asset: nil) 
        } 
       } 
       else { 
        completion(asset: nil) 
       } 
     }) 
    } 

Ich habe diesen Code versucht, zu speichern und den PHAsset zurück zu bekommen. Aber das Problem ist, dass diese PHAsset keinen Speicherort zugeordnet hat, sich fragen warum? Und was habe ich verpasst?

Ich glaube, ich muss GPS Daten nicht manuell in Bild Metadaten richtig einstellen? Ich denke, dass Photos Framework oder Asset Library kümmert sich darum. So wie Sie wissen, dass Asset Library veraltet ist, ist unsere einzige Option, Photos Framework zu verwenden. Ich lese online, dass das Speichern von Bild auf Photo Library kümmert sich darum. Ist es nicht richtig?

Gibt es eine Alternative? Sollte ich UIImageWriteToSavedPhotosAlbum Methode verwenden, um Bild zu Camera Roll zu speichern, und ich kann das sehr neue Foto zurücknehmen, das Photos Framework verwendet. Aber ich glaube nicht, dass UIImageWriteToSavedPhotosAlbum auf die Location-Sache aufpassen wird.

Haben Sie irgendwelche Gedanken?

+0

versuchen Sie, den Pfad des Bildes zu erhalten? –

+0

Ich verwende UIImagePickerController, um ein neues Foto aufzunehmen, indem ich Camera als Quelltyp im UIImagePickerController festlege. Nachdem ich das Bild ausgewählt habe, speichere ich das Bild in der Fotobibliothek und nehme es als PHAsset zurück. Dem PHAsset ist kein Ort zugeordnet, ich möchte einen Ort mit dem PHAsset. –

Antwort

2

Als Erstes allen danken, die alle freundlich waren, einen Blick in die Frage zu werfen.

Ich fand meine Antwort.

//MARK: Saving an Image to PhotoLibrary and taking back the PHAsset 

    class func savingThis(image : UIImage, completion : (asset : PHAsset?) ->()) 
    { 
     var localIdentifier : String? 
     let imageManager = PHPhotoLibrary.sharedPhotoLibrary() 

     imageManager.performChanges({() -> Void in 

      let request = PHAssetChangeRequest.creationRequestForAssetFromImage(image) 

      request.location = // Assigned current location here :) 

      if let properAsset = request.placeholderForCreatedAsset { 
       localIdentifier = properAsset.localIdentifier 
      } 
      else { 
       completion(asset: nil) 
      } 

      }, completionHandler: { (success, error) -> Void in 

       if let properLocalIdentifier = localIdentifier { 

        let result = PHAsset.fetchAssetsWithLocalIdentifiers([properLocalIdentifier], options: nil) 

        if result.count > 0 { 
         completion(asset: let asset = result[0] as? PHAsset) 
        } 
        else { 
         completion(asset: nil) 
        } 
       } 
       else { 
        completion(asset: nil) 
       } 
     }) 
    }