我有一个存储项目的数据库。这些项目可以是不同类型的类型,如文本、视频和图像。加载视图时,我从数据库中获取这些项目并将它们显示在 UITableView 中。我面临的问题与在表格视图中显示图像有关。基本上在数据库中,我存储与图片相关的 ALAsset 链接,并尝试使用以下代码获取其图像:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//...Other Code
else if ([object isMemberOfClass:[Picture class]]){
//Get a reusable cell
cell = [tableView dequeueReusableCellWithIdentifier:@"pictureCellIdentifier"];
// [self performSelectorInBackground:@selector(performAsset:) withObject:dict];
ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
{
ALAssetRepresentation *rep = [myasset defaultRepresentation];
CGImageRef iref = [rep fullResolutionImage];
if (iref) {
((PictureViewCell *)cell).placeHolderImageView.hidden = YES;
((PictureViewCell *)cell).pictureImageView.image = [UIImage imageWithCGImage:[rep fullResolutionImage] scale:[rep scale] orientation:(UIImageOrientation)[rep orientation]];
}
};
ALAssetsLibraryAccessFailureBlock failureblock = ^(NSError *myerror)
{
[Utility showAlertViewWithTitle:@"Location Error" message:@"You must activate Location Services to access the photo" cancelButtonTitle:@"Dismiss"];
};
ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init];
[assetslibrary assetForURL:[NSURL URLWithString:((Picture *)objectInArray).imagePath]
resultBlock:resultblock
failureBlock:failureblock];
//Set the background for the cell
cell.backgroundView = iv;
}
//...Other code
}
问题是,当您滑过单元格时,会调用该方法并且应用程序非常慢。所以我想有更好的方法来实现我想要做的事情。我还尝试使用performselectorInBackground:
. 性能似乎更好,但获取图像需要更多时间。
任何帮助将不胜感激。
谢谢!