2015-11-03 43 views
1

這裏我手動將一個名爲image1.png的圖像加載到iPad照片庫中,並且需要在我的iPad應用程序中獲取相同的圖像。我的圖像被替換爲具有相同名稱的新圖像,我的意圖是在不做任何代碼更改的情況下隨時更改圖像。我沒有選擇將圖像保存在服務器並從那裏訪問,所以我正在尋找這樣的解決方案。請幫我是否可以通過使用圖片名稱從iPad照片庫中獲取圖像

+0

您可以通過謂詞瀏覽PHFetchOptions()。我不確定他們是否提供圖片標題查詢。但是我們可以使用PHFetchOptions()來查詢「creationTime」,「mediatype」等。請通過以下瀏覽https://developer.apple.com/library/prerelease/ios/documentation/Photos/Reference/PHFetchOptions_Class/index.html –

回答

0

我沒有得到解決方案,使用圖像名稱從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]; 

      } 

     } 
    } 
} 

使用此函數我解決了我的問題。它可能是有用的。

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

+0

我不想使用應用程序保存圖像,我想加載無論何時用戶需要,都可以手動將圖像存儲到庫中,並且還可以替換圖像,名稱應該與「image1.png」相同。 – Rajith

+0

@Rajith asfaik這是不可能的,因爲你不能設置圖像的名稱。您可以隨時獲取[相機膠捲中的最新圖像](http://stackoverflow.com/a/8872425/1633733)。您也可以啓用與iTunes共享文件,結果用戶將能夠在iTunes中查看和交換文件。 – limfinity

相關問題