1

Here I am loading one image named image1.png into iPad Photo library manually and need to get same image in my iPad app. Image my get replaced with new image with same name, My intention is to change the image whenever needed without doing any code change. I don't have option to save the images in server and access from there so I am looking for solution like this. Please help me

Rajith
  • 59
  • 6
  • You can go through PHFetchOptions() with predicate. I am not sure whether they are providing query on image title or not. But we can make query on "creationTime","mediatype" etc using PHFetchOptions(). Please go through below https://developer.apple.com/library/prerelease/ios/documentation/Photos/Reference/PHFetchOptions_Class/index.html – Gagan_iOS Nov 03 '15 at 14:19

2 Answers2

1

You might want to save it to the local storage:

// Assuming 'newImage' is a 'UIImage' object containing your image
NSData *imageData = UIImagePNGRepresentation(newImage);

NSArray *paths =      NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *imagePath =[documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png",@"yourImageName"]];

NSLog((@"pre writing to file"));
if (![imageData writeToFile:imagePath atomically:NO]) 
{
    NSLog((@"Failed to cache image data to disk"));
}
else
{
    NSLog((@"the cachedImagedPath is %@",imagePath)); 
    // Persist path in a dictionary or NSUserDefaults
}

And retrieve it later like this:

NSString *theImagePath = [yourDictionary objectForKey:@"cachedImagePath"];
UIImage *customImage = [UIImage imageWithContentsOfFile:theImagePath];

Code from here.

Community
  • 1
  • 1
limfinity
  • 726
  • 6
  • 17
  • I don't want to save the image using the app,I want to load the images manually into the library whenever user need and also he can replace the image and the name should be same like "image1.png". – Rajith Nov 04 '15 at 05:32
  • @Rajith asfaik this is not possible, because you can't set the image name. You can always get the [latest image in the camera roll](http://stackoverflow.com/a/8872425/1633733). Also you can enable file sharing with iTunes in the consequence the user will be able to see and exchange the file in iTunes. – limfinity Nov 04 '15 at 08:44
0

I didn't got solution to load image from iPad library using image name, so i saved all my images in one user library and loaded that images from my iPad app.

-(void)loadImageFromLibrary
{
   __block UIImage *ima;

    NSMutableArray *images=[NSMutableArray new];

    PHAsset *asset = nil;

    PHAssetCollection *collection;

    NSArray *collectionsFetchResults;

    NSMutableArray *localizedTitles = [[NSMutableArray alloc] init];

    PHFetchResult *userCollections = [PHCollectionList fetchTopLevelUserCollectionsWithOptions:nil];

    collectionsFetchResults = @[userCollections];//to get images from user created library.

    for (int i = 0; i < collectionsFetchResults.count; i ++) {

        PHFetchResult *fetchResult = collectionsFetchResults[i];

        for (int x = 0; x < fetchResult.count; x ++) {

            collection = fetchResult[x];
            localizedTitles[x] = collection.localizedTitle;//get library names
        }

    }//MyImages is my image library
    if ([collection.localizedTitle isEqualToString:@"MyImages"]) {

        PHFetchResult *fetchResult = [PHAsset fetchAssetsInAssetCollection:collection options:nil];

        if (fetchResult != nil && fetchResult.count > 0) {

            for (asset in fetchResult) {

                PHImageRequestOptions * imageRequestOptions = [[PHImageRequestOptions alloc] init];

                imageRequestOptions.synchronous = YES;

                [[PHImageManager defaultManager] requestImageForAsset:asset

                                                           targetSize:PHImageManagerMaximumSize

                                                          contentMode:PHImageContentModeDefault

                                                              options:imageRequestOptions

                                                        resultHandler:^void(UIImage *image, NSDictionary *info) {

                                                            NSLog(@"info = %@", info);

                                                            ima = image;

                                                        }];

                [images addObject:ima];

            }

        }
    }
}

Using this function i solved my problem.It may be useful.

Rajith
  • 59
  • 6