【问题标题】:Is it possible to get an image from iPad Photo Library by using the image name是否可以使用图像名称从 iPad 照片库中获取图像
【发布时间】:2016-02-03 17:04:39
【问题描述】:

在这里,我将一个名为 image1.png 的图像手动加载到 iPad 照片库中,并且需要在我的 iPad 应用程序中获取相同的图像。图像我被替换为具有相同名称的新图像,我的目的是在需要时更改图像而不进行任何代码更改。我没有选择将图像保存在服务器中并从那里访问,所以我正在寻找这样的解决方案。请帮帮我

【问题讨论】:

标签: ios objective-c image ipad


【解决方案1】:

您可能希望将其保存到本地存储:

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

稍后像这样检索它:

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

来自here的代码。

【讨论】:

  • 我不想使用应用程序保存图像,我想在用户需要时手动将图像加载到库中,并且他可以替换图像并且名称应该与“image1”相同.png"。
  • @Rajith asfaik 这是不可能的,因为您无法设置图像名称。你总是可以得到latest image in the camera roll。您还可以启用与 iTunes 的文件共享,这样用户将能够在 iTunes 中查看和交换文件。
【解决方案2】:

我没有使用图像名称从 iPad 库加载图像的解决方案,因此我将所有图像保存在一个用户库中并从我的 iPad 应用程序加载这些图像。

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

            }

        }
    }
}

使用这个功能我解决了我的问题。它可能很有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-08
    • 1970-01-01
    • 2012-04-04
    相关资源
    最近更新 更多