UIImage Saving image with file name on the iPhone

南笙酒味 提交于 2019-11-26 22:53:28

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]; 

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.

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;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!