【问题标题】:How to Save Image to Application Folder from UIImagePickerController?如何从 UIImagePickerController 将图像保存到应用程序文件夹?
【发布时间】:2015-04-14 06:48:25
【问题描述】:

我想到了一个让我发疯的问题。我已经搜索了很多来解决它,但是我找到的所有答案都是旧的并且没有帮助。

我目前正在开发使用 UIImagePickerControllerDelegatedidFinishPickingMediaWithInfo 的应用程序,我想将所选图像保存到应用程序文件夹。

到目前为止,我可以用这个选择图像;

- (IBAction)btnPressed:(UIButton *)sender
{
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum])
    {
        UIImagePickerController *imgPicker = [[UIImagePickerController alloc] init];
        imgPicker.delegate = self;
        imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        imgPicker.mediaTypes = [NSArray arrayWithObject:(NSString *)kUTTypeImage];
        imgPicker.allowsEditing = NO;

        [self presentViewController:imgPicker animated:YES completion:nil];
    }

}

但无法使用 didFinishPickingMediaWithInfo 将其保存到应用程序;

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

    NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
    [self dismissViewControllerAnimated:YES completion:nil];
    if ([mediaType isEqualToString:(NSString *) kUTTypeImage])
    {
        UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];

        //Assigning the selected image to imageView to see on UI side.
        imageViewerImage.image = image;
    }
}

我想知道这一点之后的部分应该是什么。

感谢您提供的任何帮助。

【问题讨论】:

  • 所以问题是如何将 UIImage 写入文件夹中的文件。
  • 是的@gabbler 这正是我要找的。​​span>
  • 你没有搜索很多来解决它,否则你会找到它,因为它是“将图像保存到 ios 中的文档文件夹”的第一个搜索结果 - stackoverflow.com/questions/2037432/…

标签: ios objective-c iphone ios8 uiimagepickercontroller


【解决方案1】:

我在我的项目中这样做了:

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    UIImage * imageDone = [info objectForKey:@"UIImagePickerControllerEditedImage"];
    [self saveImage:imageDone];
    [self.popover dismissPopoverAnimated:YES];
}

-(void)saveImage:(UIImage*)image{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
   NSString *documentsDirectory = [paths objectAtIndex:0];    
   NSString * imgpath = [documentsDirectory stringByAppendingFormat:@"/media/img/"];

   long x = arc4random() % 10000;
   NSString * namePic = [NSString stringWithFormat:@"%lld%ld-photo.jpg", self.idToLoad.longLongValue, x];
   NSString * stringName = [NSString stringWithFormat:@"%@%@", imgpath, namePic];
   [UIImageJPEGRepresentation(image, 0.3) writeToFile:stringName atomically:YES];
}

【讨论】:

    【解决方案2】:
    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
    {
    
        [self dismissViewControllerAnimated:YES completion:NULL];
    
    UIImage* image;
    
    if([[info valueForKey:@"UIImagePickerControllerMediaType"] isEqualToString:@"public.image"])
        {
    
      image = [info valueForKey:@"UIImagePickerControllerOriginalImage"];
    
     NSString *stringPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:@"New Folder"];
    
    // New Folder is your folder name
    
     NSError *error = nil;
    
    if (![[NSFileManager defaultManager] fileExistsAtPath:stringPath])
        [[NSFileManager defaultManager] createDirectoryAtPath:stringPath withIntermediateDirectories:NO attributes:nil error:&error];
    
    NSString *fileName = [stringPath stringByAppendingFormat:@"/image.jpg"];
    
    NSData *data = UIImageJPEGRepresentation(image, 1.0);
    
    [data writeToFile:fileName atomically:YES];
    
       }
    }
    

    【讨论】:

    • 像魅力一样工作!我已经根据自己的需要进行了一些配置,但毕竟它对我有用。
    • 不要对键使用字符串文字。 Apple 为这些值提供了适当的常量。使用它们。示例:使用UIImagePickerControllerOriginalImage 而不是@"UIImagePickerControllerOriginalImage"
    【解决方案3】:
    //In Your - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage (UIImage *)image editingInfo:(NSDictionary *)editingInfo . Write this code
    
    NSData *pngData = UIImagePNGRepresentation(image);
    
    //This pulls out PNG data of the image you've captured. From here, you can write it to a file:
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
    NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory 
    NSString *filePath = [documentsPath stringByAppendingPathComponent:@"image.png"]; //Add the file name
    [pngData writeToFile:filePath atomically:YES]; //Write the file
    

    【讨论】:

      【解决方案4】:

      只需将图像保存在您的应用文档目录中即可:

      // first find the path in which to save the image :
      NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
      NSString *baseDir = [paths objectAtIndex:0];
      
      // then save image as JPEG
      NSString* filename = [baseDir stringByAppendingPathComponent:@"filename.jpg"];
      [UIImageJPEGRepresentation(image, 1.0) writeToFile:jpgPath atomically:YES];
      // or PNG
      NSString* filename = [baseDir stringByAppendingPathComponent:@"filename.png"];
      [UIImagePNGRepresentation(image) writeToFile:pngPath atomically:YES];
      

      【讨论】:

        【解决方案5】:

        将 UIImage 保存到文档文件夹,如下所示:

        UIImage* myUIImage = ...
        NSData *pngData = UIImagePNGRepresentation(myUIImage);
        //NSData *pngData = UIImageJPEGRepresentation(myUIImage,0.5); //alternative comressed jpg instead of png
        NSString *filePath = [[self buildDocumentsPath] stringByAppendingPathComponent:@"pictureName.png"]; //Add the file name
        [pngData writeToFile:filePath atomically:YES]; //Write the file
        

        以字符串形式获取文档的路径:

        - (NSString *)buildDocumentsPath{
            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
            NSString *documentsPath = [paths objectAtIndex:0];
            return documentsPath;
        }
        

        【讨论】:

          【解决方案6】:

          试试这个代码。它可能会帮助你。 :)

          - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
          {
              imgViewProfile.image = info[UIImagePickerControllerEditedImage];
              [picker dismissViewControllerAnimated:YES completion:^{ }];
              // ------ Now save this image to document directory by getting its path
          }
          

          【讨论】:

            猜你喜欢
            • 2011-06-24
            • 2014-06-13
            • 2018-05-30
            • 2018-04-01
            • 2021-11-06
            • 2011-10-12
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多