【问题标题】:save image to custom folder titanium将图像保存到自定义文件夹钛
【发布时间】:2016-04-28 10:37:11
【问题描述】:

我开发了一个应用程序来从手机摄像头拍照。但是现在我需要将手机内存中的图像存储到我创建的文件夹中。

我试过这个:

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

但它没有出现在画廊中。如何将图像存储到手机内存中,如何在手机内存中创建一个服装文件夹来存储图像?

【问题讨论】:

    标签: android ios iphone swift titanium


    【解决方案1】:
     - (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];
          }
    
                }
    

    这会将图像保存在您拍摄的相机胶卷中

    【讨论】:

      【解决方案2】:

      这将创建相册并保存到此相册中。

      我想出了这个单例类来处理它:

      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)
      }
      

      }

      当您第一次实例化该类时,如果自定义相册尚​​不存在,则会创建它。您可以像这样保存图像:

      CustomPhotoAlbum.sharedInstance.saveImage(image)
      

      注意:CustomPhotoAlbum 类假定应用程序已经有权访问照片库。处理权限有点超出这个问题/答案的范围。 所以在使用之前请确保 PHPhotoLibrary.authorizationStatus() == .Authorize。并在必要时请求授权。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-20
        • 2012-12-07
        • 1970-01-01
        • 1970-01-01
        • 2012-04-19
        • 1970-01-01
        相关资源
        最近更新 更多