我有一個基於Xcode objective-c的應用程序,它有一個使用PHP從MySql加載圖像和一些信息的tableview。除了圖像以外,一切都很好,當我向下滾動表格需要很長時間時,它會將圖像(圖像A在圖像B中)混合幾秒鐘,然後變爲其位置,特別是當我快速滾動表格時:在UITableView中加載時混合的圖像
她是我的代碼:
- (void)downloadImageWithURL:(NSURL *)url completionBlock:(void (^)(BOOL succeeded, UIImage *image))completionBlock
{
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if (!error)
{
UIImage *image = [[UIImage alloc] initWithData:data];
completionBlock(YES,image);
} else{
completionBlock(NO,nil);
}
}];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *one = listObjects.theUserId; // the listObjects where is store my info and the images Url
NSURL *myUrl = [NSURL URLWithString:one];
if (listObjects.image) {
cell.myImageView.image = listObjects.image;
} else {
// set default user image while image is being downloaded
cell.myImageView.image = [UIImage imageNamed:@"defaultImage.png"];
// download the image asynchronously
[self downloadImageWithURL:myUrl completionBlock:^(BOOL succeeded, UIImage *image) {
if (succeeded) {
// change the image in the cell
cell.myImageView.image = image;
// cache the image for use later (when scrolling up)
listObjects.image = image;
}
}];
}
return cell;
}
請檢查我的代碼,我需要補充? 謝謝
我有類似的問題。如果有任何問題,我會及時通知你。順便說一句我已經嘗試使用 - (void)prepareForReuse並且仍然沒有改變。 – 2015-04-06 21:13:42