2016-04-28 8 views
1

Ich habe eine App entwickelt, um ein Foto von der Telefonkamera zu machen. Aber jetzt muss ich das Bild im Telefonspeicher in einem von mir erstellten Ordner speichern.Bild in benutzerdefinierten Ordner speichern Titan

Ich habe dies versucht:

var filename = Titanium.Filesystem.resourcesDirectory + "/newImageFile.jpg"; 
var imageFile = Titanium.Filesystem.getFile(filename); 
imageFile.write(capturedImg); 

Aber es in der Galerie nicht apear nicht. Wie kann ich das Bild im Telefonspeicher speichern und wie kann ich einen Kostümordner im Telefonspeicher erstellen, um das Bild zu speichern?

Antwort

0

Dies wird Fotoalbum erstellen und in diesem Album speichern.

ich kam mit diesen Singletonklasse bis es zu handhaben:

import Photos 

class CustomPhotoAlbum { 

static let albumName = "Titanium" 
static let sharedInstance = CustomPhotoAlbum() 

var assetCollection: PHAssetCollection! 

init() { 

    func fetchAssetCollectionForAlbum() -> PHAssetCollection! { 

     let fetchOptions = PHFetchOptions() 
     fetchOptions.predicate = NSPredicate(format: "title = %@", CustomPhotoAlbum.albumName) 
     let collection = PHAssetCollection.fetchAssetCollectionsWithType(.Album, subtype: .Any, options: fetchOptions) 

     if let firstObject: AnyObject = collection.firstObject { 
      return collection.firstObject as! PHAssetCollection 
     } 

     return nil 
    } 

    if let assetCollection = fetchAssetCollectionForAlbum() { 
     self.assetCollection = assetCollection 
     return 
    } 

    PHPhotoLibrary.sharedPhotoLibrary().performChanges({ 
     PHAssetCollectionChangeRequest.creationRequestForAssetCollectionWithTitle(CustomPhotoAlbum.albumName) 
    }) { success, _ in 
     if success { 
      self.assetCollection = fetchAssetCollectionForAlbum() 
     } 
    } 
} 

func saveImage(image: UIImage) { 

    if assetCollection == nil { 
     return // If there was an error upstream, skip the save. 
    } 

    PHPhotoLibrary.sharedPhotoLibrary().performChanges({ 
     let assetChangeRequest = PHAssetChangeRequest.creationRequestForAssetFromImage(image) 
     let assetPlaceholder = assetChangeRequest.placeholderForCreatedAsset 
     let albumChangeRequest = PHAssetCollectionChangeRequest(forAssetCollection: self.assetCollection) 
     albumChangeRequest.addAssets([assetPlaceholder]) 
    }, completionHandler: nil) 
} 

}

Wenn Sie zuerst die Klasse instanziiert, wird das benutzerdefinierte Album erstellt werden, wenn es nicht bereits vorhanden ist. Sie können ein Bild wie folgt speichern:

CustomPhotoAlbum.sharedInstance.saveImage(image) 

HINWEIS: Die CustomPhotoAlbum Klasse übernimmt die App hat bereits die Erlaubnis, die Foto-Bibliothek zugreifen. Der Umgang mit den Berechtigungen ist etwas außerhalb des Rahmens dieser Frage/Antwort. So stellen Sie sicher, dass PHPhotoLibrary.authorizationStatus() == .Authorize, bevor Sie es verwenden. Und wenn nötig Autorisierung anfordern.

0
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info 
      { 
      NSData *imageData = [info objectForKey:UIImagePickerControllerOriginalImage]; 
    if(picker.sourceType==UIImagePickerControllerSourceTypeCamera) 
     { 
     //to save camera roll 
      [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{ 
      [PHAssetChangeRequest creationRequestForAssetFromImage:image]; 
              } completionHandler:nil]; 
     } 

      } 

Dies wird Bild in Kamerarolle speichern, die von Ihnen genommen wird

Verwandte Themen