我有一個iOS
項目,它從數據源方法中提取圖像。我希望能夠從assets library
(和下面的代碼塊這樣做就好)拉圖像。iOS - 從塊中返回一個變量
但是,我需要這個dataSource
方法返回UIImage
,但是當我使用資產庫方法獲取圖像時,圖像返回到結果塊中。簡單地將return image
放在結果塊中顯然不起作用。
有沒有人有任何想法如何我可以從結果塊內返回UIImage
?我還看到了其他幾個關於在塊內返回圖像的問題,但他們說要調用另一種方法。我 - 不幸 - 不能這樣做,因爲這種方法是一個nimbus數據源方法,它必須返回一個UIImage。
任何幫助或建議將不勝感激!代碼如下:
- (UIImage *)photoAlbumScrollView: (NIPhotoAlbumScrollView *)photoAlbumScrollView
photoAtIndex: (NSInteger)photoIndex
photoSize: (NIPhotoScrollViewPhotoSize *)photoSize
isLoading: (BOOL *)isLoading
originalPhotoDimensions: (CGSize *)originalPhotoDimensions {
__block UIImage *image = nil;
ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init];
[assetslibrary assetForURL:[_photos objectAtIndex:photoIndex]
resultBlock:^(ALAsset *asset){
ALAssetRepresentation *rep = [asset defaultRepresentation];
CGImageRef imageRef = [rep fullScreenImage];
if (imageRef) {
image = [UIImage imageWithCGImage:imageRef];
}
}
failureBlock:^(NSError *error) {
//return nil;
}];
return image;
}
請問您可以提供更多關於這個dataSource方法的代碼嗎?你上面的代碼顯然是功能性的,但是你說如果來自dataSource沒有辦法發送消息 - 我必須相信我並不是唯一不知道真正問題在這一點上的人。 –
返回的圖像始終爲零,因爲assetslibary調用是異步的。所以在'resultBlock'執行之前,返回的圖像被觸發。在'image = [UIImage imageWithCGImage:imageRef]'後面的'resultBlock'中放置一個'return image'似乎是非法的;' – Brett
我想你必須重新加載你的架構才能在查詢你的圖片之前加載圖片數據源。 –