UIImage Saving image with file name on the iPhone

后端 未结 3 1208
迷失自我
迷失自我 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条回答
  •  猫巷女王i
    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;
    }
    

提交回复
热议问题