這裏我手動將一個名爲image1.png的圖像加載到iPad照片庫中,並且需要在我的iPad應用程序中獲取相同的圖像。我的圖像被替換爲具有相同名稱的新圖像,我的意圖是在不做任何代碼更改的情況下隨時更改圖像。我沒有選擇將圖像保存在服務器並從那裏訪問,所以我正在尋找這樣的解決方案。請幫我是否可以通過使用圖片名稱從iPad照片庫中獲取圖像
1
A
回答
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。
相關問題
- 1. 我可以從iPhone中的照片獲取圖像的名稱
- 2. 從照片庫使用照片框架獲取所有圖像
- 3. 從照片庫中獲取圖像名稱
- 4. 是否可以通過使用Javascript的圖片url獲取圖片大小
- 5. iPad模擬器iOS6,沒有從UIImagePickerController照片庫獲取圖像
- 6. IOS圖片庫獲取給定照片的相冊名稱
- 7. 獲取圖片名稱的iPhone照片庫
- 8. 如何獲取可繪製圖片的照片名稱?
- 9. 從照片庫中獲取照片
- 10. 從照片庫中獲取圖片並通過名稱和路徑將其上傳到網絡服務
- 11. 嘗試通過圖像選擇器從照片庫獲取照片時,應用程序崩潰
- 12. 是否有可能從圖片類獲取圖片令牌?
- 13. 是否可以檢查圖像是由相機拍攝還是通過JavaScript從照片庫上傳?
- 14. 從照片庫中加載圖像
- 15. 從Android照片庫中獲取所有圖片
- 16. iphone sdk如何從照片庫中獲取完整的圖像?
- 17. 如何從照片庫中獲取圖像?
- 18. 用圖api獲取照片
- 19. [iOS]從照片庫中選擇圖片
- 20. startActivityfor從片段結果 - 從圖片庫中獲取圖片
- 21. 使用xctest從照片/圖庫中挑選圖像
- 22. 從圖庫中提取照片android
- 23. 無法從圖庫中選取照片?
- 24. 可通過API獲取照片位置?
- 25. 使用圖形API從帶標記的照片中獲取相冊的名稱
- 26. 使用UIImagepickercontroller從iPhone照片庫獲取照片
- 27. 從iphone照片庫中獲取照片並在表格視圖中顯示
- 28. FB.ui的屬性'圖片'可以從HttpHandler中獲取圖像嗎?
- 29. 從圖片名稱
- 30. 從wordpress圖片庫獲取一張照片
您可以通過謂詞瀏覽PHFetchOptions()。我不確定他們是否提供圖片標題查詢。但是我們可以使用PHFetchOptions()來查詢「creationTime」,「mediatype」等。請通過以下瀏覽https://developer.apple.com/library/prerelease/ios/documentation/Photos/Reference/PHFetchOptions_Class/index.html –