UIImage Saving image with file name on the iPhone

后端 未结 3 1188
迷失自我
迷失自我 2020-11-28 13:50

How can I save an image (like using UIImageWriteToSavedPhotosAlbum() method) with a filename of my choice to the private/var folder?

相关标签:
3条回答
  • 2020-11-28 14:15

    UIImageWriteToSavedPhotosAlbum() is only used for saving to the photos camera roll. To save to a custom folder, you need to convert the UIImage into NSData with UIImageJPEGRepresentation() or UIImagePNGRepresentation(), then save this NSData to anywhere you like.

    0 讨论(0)
  • 2020-11-28 14:27

    You can use below code, without use of 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;
    }
    
    0 讨论(0)
  • 2020-11-28 14:37

    Kenny, you had the answer! For illustration I always think code is more helpful.

    //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]; 
    
    0 讨论(0)
提交回复
热议问题