How can I save an image (like using UIImageWriteToSavedPhotosAlbum() method) with a filename of my choice to the private/var folder?
Asked
Active
Viewed 2.4k times
3 Answers
17
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];

erastusnjuki
- 1,491
- 3
- 14
- 32
-
2A sample saving to the documents folder NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *filePath2 = [NSString stringWithFormat:@"%@/%@.jpg", documentsDirectory, fileName]; NSData* imageData = UIImageJPEGRepresentation(drawImage.image, 1.0); [imageData writeToFile:filePath2 atomically:NO]; – Robert Childan Nov 21 '10 at 23:31
-
[imageData writeToFile:@"/private/var/mobile/Media/DCIM/100APPLE/customImageFilename.jpg" atomically:NO]; What is this private/var/mobile folder? Is this a standard place to write out a file in ios? – user798719 Aug 01 '11 at 22:58
-
3The question is, if apple will allow an app with that code... http://stackoverflow.com/questions/2884003/will-the-app-get-rejected-if-you-write-image-to-private-var-mobile-media-dcim-1?lq=1 – Chris Jan 24 '13 at 10:24
-
Is this still allowed..? Doesn't seem to be a filepath I have write access to. – James Bedford Mar 27 '17 at 17:03
10
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.

kennytm
- 510,854
- 105
- 1,084
- 1,005
0
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;
}

Dimple Shah
- 304
- 3
- 18