UIImage Saving image with file name on the iPhone

a 夏天 提交于 2019-11-26 08:27:49

问题


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


回答1:


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



回答2:


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.




回答3:


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


来源:https://stackoverflow.com/questions/2264039/uiimage-saving-image-with-file-name-on-the-iphone

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