1

Already have answers, for saving images into specific folder by using ALAssetsLibrary. But it is deprecated in iOS 9.0 and giving warning "Use PHPhotoLibrary from the Photos framework instead".

When i change ALAssetsLibrary into PHPhotoLibrary getting the error at this point.

    [self.library saveImage:image toAlbum:@"myAlbum" withCompletionBlock:^(NSError *error) {
    if (error!=nil) {
        NSLog(@"Big error: %@", [error description]);
    }
}];

*error

 No visible @interface for 'PHPhotoLibrary' declares the selector 'saveImage:toAlbum:withCompletionBlock:' 

How to save images into specific folder By Using PHPhotoLibrary (or) any other solutions.

Thank you in advance.

Chandan Reddy
  • 221
  • 1
  • 5
  • 19
  • You may want to also refer to http://stackoverflow.com/questions/28348450/phphotolibrary-error-while-saving-image-at-url for some discussion on the topic specifically using PHPhotoLibrary as an API. – dokun1 Nov 09 '15 at 08:07
  • seen that link... saving images is ok ...but my requirement is to save into specific folder – Chandan Reddy Nov 09 '15 at 09:06
  • check my answer below, updated to show how to put binary image data into specific folders. – dokun1 Nov 09 '15 at 09:10

2 Answers2

3

I have not used PHPhotoLibrary yet but I know the old school way of saving photos in Obj-C using the local documents directory:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = paths.firstObject;
NSData *imageData = UIImagePNGRepresentation(image);

NSString *imagePath = [NSString stringWithFormat:@"%@/%@",documentsDirectory,imageFolder];

BOOL isDir;
NSFileManager *fileManager= [NSFileManager defaultManager];
if(![fileManager fileExistsAtPath:imagePath isDirectory:&isDir])
    if(![fileManager createDirectoryAtPath:imagePath withIntermediateDirectories:YES attributes:nil error:NULL])
        NSLog(@"Error: folder creation failed %@", documentsDirectory);

[[NSFileManager defaultManager] createFileAtPath:[NSString stringWithFormat:@"%@/%@", imagePath, imageName] contents:nil attributes:nil];
[imageData writeToFile:[NSString stringWithFormat:@"%@/%@", imagePath, imageName] atomically:YES];

To retrieve that image from the NSDocumentDirectory:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = paths.firstObject;
NSString *fullPath = [NSString stringWithFormat:@"%@/%@", documentDirectory, imagePath]; // image path is same as above
if ([[NSFileManager defaultManager] fileExistsAtPath:fullPath]) {
    UIImage *image = [UIImage imageWithContentsOfFile:fullPath];
    return image;
} else {
    return nil;
}
dokun1
  • 2,120
  • 19
  • 37
0

Because there is not Method named like this.

For saving images to the PHPhotoLibrary you need to create an Asset. See

Which PHAssetCollection to use for saving an image?

for detailed informations.

Community
  • 1
  • 1
Thallius
  • 2,482
  • 2
  • 18
  • 36