【问题标题】:UIImage Saving image with file name on the iPhoneUIImage 在 iPhone 上保存带有文件名的图像
【发布时间】:2011-01-16 20:50:07
【问题描述】:

如何将带有我选择的文件名的图像(例如使用 UIImageWriteToSavedPhotosAlbum() 方法)保存到 private/var 文件夹?

【问题讨论】:

    标签: iphone uiimage uiimagepickercontroller sandbox save-image


    【解决方案1】:

    UIImageWriteToSavedPhotosAlbum() 仅用于保存到照片相机胶卷。要保存到自定义文件夹,您需要使用UIImageJPEGRepresentation()UIImagePNGRepresentation() 将 UIImage 转换为 NSData,然后将此 NSData 保存到您喜欢的任何位置。

    【讨论】:

      【解决方案2】:

      肯尼,你有答案!为了说明,我一直认为代码更有帮助。

      //I do this in the didFinishPickingImage:(UIImage *)img method
      
      NSData* imageData = UIImageJPEGRepresentation(img, 1.0);
      
      
      //save to the default 100Apple(Camera Roll) folder.   
      
      [imageData writeToFile:@"/private/var/mobile/Media/DCIM/100APPLE/customImageFilename.jpg" atomically:NO]; 
      

      【讨论】:

      • 保存到文档文件夹 NSArray 的示例 paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *filePath2 = [NSString stringWithFormat:@"%@/%@.jpg", documentsDirectory, fileName]; NSData imageData = UIImageJPEGRepresentation(drawImage.image, 1.0); [imageData writeToFile:filePath2 atomically:NO];
      • [imageData writeToFile:@"/private/var/mobile/Media/DCIM/100APPLE/customImageFilename.jpg" atomically:NO];这个 private/var/mobile 文件夹是什么?这是在 ios 中写出文件的标准位置吗?
      • 问题是,如果苹果允许使用该代码的应用程序...stackoverflow.com/questions/2884003/…
      • 这还允许吗..?似乎不是我有权写入的文件路径。
      【解决方案3】:

      您可以使用以下代码,而无需使用 ALAssetsLibrary...

      NSString *fileName;
      
      - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
      {
      [picker dismissViewControllerAnimated:YES completion:^{
      
      if( [picker sourceType] == UIImagePickerControllerSourceTypeCamera )
                  {
                      UIImageWriteToSavedPhotosAlbum(image,nil, nil, nil);
                      [self performSelector:@selector(GetImageName) withObject:nil afterDelay:0.5];
                  }
                  else
                  {
                      NSURL *refURL = [info valueForKey:UIImagePickerControllerReferenceURL];
                      PHFetchResult *result = [PHAsset fetchAssetsWithALAssetURLs:@[refURL] options:nil];
                      fileName = [[result firstObject] filename];
                  }
      }];
      
      -(void)GetImageName
      {
       NSString *str =@"";
       PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init];
       fetchOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];
       PHFetchResult *fetchResult = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:fetchOptions];
      
      if (fetchResult != nil && fetchResult.count > 0) {
      
          str = [[fetchResult lastObject] filename];
      }
      
      fileName = str;
      }
      

      【讨论】:

        猜你喜欢
        • 2010-11-12
        • 1970-01-01
        • 2016-12-07
        • 1970-01-01
        • 2012-04-30
        • 2014-05-13
        • 2012-06-27
        • 2016-11-15
        相关资源
        最近更新 更多