[英]smooth scrolling UITableView iPhone, with images
編輯:閱讀下面的答案並查看了我現在迷路的所有指南之后,我實在是個菜鳥而已。
我不想為我完成編碼,我需要一些關於如何設置單獨線程,然后將其引用到tableView的明確建議。
任何NOOB教程?!?
這是我為將圖像放入tableView設置的代碼。 所有圖像均加載,但僅在滾動表時加載。
如何停止呢?
任何幫助,將不勝感激。
NSString *userImage = [(Tweet*)[profile objectAtIndex:indexPath.row] profileImage];
dispatch_async(dispatch_get_global_queue(0, 0), ^{
NSData* imageData = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:[(Tweet*)[profile objectAtIndex:indexPath.row] profileImage]]];
dict =[[NSMutableDictionary alloc]init];
//UIImage *retImage =[dict objectForKey:(@"%@", userImage)];
UIImage *retImage =[dict objectForKey:userImage];
dispatch_async(dispatch_get_main_queue(), ^{
if (!retImage) {
UIImage *profileImage=[UIImage imageWithData:imageData];
[dict setObject:profileImage forKey:userImage];
}
UIImage *retImage2 =[dict objectForKey:userImage];
photo.image = retImage2;
[imageData release];
});
將圖像加載到后台線程是一種選擇。 更好的替代方法是使用自動處理后台線程的NSOperationQueue。
蘋果公司提供了同樣的樣品。
希望對您有所幫助:)
編輯:如果您不知道或不想使用線程,那么還有另一種選擇。 看看這個: 沒有線程的表的下載圖像
您的圖片有多大? 您可能需要先縮小它們的大小,然后再將它們放入tableView中。
就像是:
#define kImageSize 44
// Create a thumbnail version of the image for the oject.
CGSize size = newImage.size;
CGFloat ratio ;
if (size.width > size.height) {
ratio = kImageSize / size.width;
} else {
ratio = kImageSize/ size.height;
}
CGRect rect = CGRectMake(0.0, 0.0, ratio * size.width, ratio * size.height);
if (NULL != UIGraphicsBeginImageContextWithOptions) { //test that function is available
UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0); //allow iphone4 to use higher-res
} else {
UIGraphicsBeginImageContext(rect.size);
}
[newImage drawInRect:rect];
userThumbnailImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
另外,我注意到每次圖像在詞典中時,您都將再次存儲; 您可能想將緩存行放回(!photo.image)子句中。
在后台線程中下載圖像,並在圖像可用時將其設置到表視圖單元格中的UIImageView中。 您正在通過網絡加載圖像而使主線程停滯不前。
[已解決]我添加了一個if語句,它不是最佳選擇,但僅在圖片不存在時才滯后。
if (!image) {
[activityIndicator startAnimating];
UIImage *phoneImage=[UIImage imageWithData:[[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:userImage]]];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* path = [documentsDirectory stringByAppendingPathComponent:
[NSString stringWithFormat:@"%@:%@", imageName, fileName] ];
NSData* data = UIImagePNGRepresentation(phoneImage);
[data writeToFile:path atomically:YES];
}
else{
photo.image=image;
[activityIndicator stopAnimating];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.