2014-04-21 63 views
0

我有一個UItableView,它分別列出了插座數量,每個插座都有一個徽標圖像。我將這些圖像保存在iPhone本地,所以如果在iPhone上找到任何圖像,它將從那裏獲取,如果沒有,那麼它會發送服務調用和獲取數據。它正在相應地工作,但是當我在桌面上向下滾動時,它在圖像下載時的某個時刻被掛起,因爲它被下載,它再次正常工作。有沒有什麼解決方案可以執行下載圖像的多任務處理,並且可以通過服務調用或本地獲取來表示它們。在iOS中下載圖像的多任務處理sdk

這裏是我的代碼..

//下載網點的圖像局部

+0

我建議你使用SDWebImage庫來提高性能,而不是自己進行這些處理。 – vin

回答

1

讓我們嘗試:

//When you download something, execute in background thread 
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), 
       ^{ 
        //download some thing 

       dispatch_async(dispatch_get_main_queue(), ^{ 
        // when download finish execute in main thread to update data 
        // this function should be called in response of downloading 
       }); 
       }); 

希望這有助於你。

+0

這對我有用,謝謝:) – user3422986

1

下載圖片在後臺將解決您的問題在這裏

試試這個。

if(!fileExists) 
{ 
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
     NSData *data1 = [NSData dataWithData:UIImagePNGRepresentation(image)]; 
     [data1 writeToFile:pngFilePath atomically:YES]; 
    }); 
} 
0

您可以使用此代碼集成ImageCache來下載異步和緩存圖像。這部分代碼顯示在UITableView中下載圖像。或者你可以在GitHub上這些文件看看這個項目SDImageCache

if ([[ImageCache sharedImageCache] DoesExist:photoURL] == true) 
{ 
    image = [[ImageCache sharedImageCache] GetImage:photoURL]; 
    cell.imageView.image = image; 
} 
else 
{ 
    cell.imageView.image = [UIImage imageNamed:@"PlaceHolder"]; 
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul); 
    dispatch_async(queue, ^(void) { 

     NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:photoURL]]; 

     UIImage* image = [[UIImage alloc] initWithData:imageData]; 
     if (image) { 
      dispatch_async(dispatch_get_main_queue(), ^{ 
       if (cell.tag == indexPath.section) { 
        cell.imageView.image = image; 
        [cell setNeedsLayout]; 
        [[ImageCache sharedImageCache] AddImage:image imageURL:photoURL]; 
       } 
      }); 
     } 
    }); 
} 

導入到你的項目

ImageCache.h

#import <Foundation/Foundation.h> 

@interface ImageCache : NSObject 

@property (nonatomic, retain) NSCache *imgCache; 

#pragma mark - Methods 

+ (ImageCache*)sharedImageCache; 
- (void) AddImage:(UIImage *)image imageURL:(NSString *)imageURL; 
- (UIImage*) GetImage:(NSString *)imageURL; 
- (BOOL) DoesExist:(NSString *)imageURL; 


@end 

ImageCache.m

#import "ImageCache.h" 


@implementation ImageCache 

@synthesize imgCache; 

#pragma mark - Methods 

static ImageCache* sharedImageCache = nil; 

+(ImageCache*)sharedImageCache 
{ 
    @synchronized([ImageCache class]) 
    { 
     if (!sharedImageCache) 
      sharedImageCache= [[self alloc] init]; 

     return sharedImageCache; 
    } 

    return nil; 
} 

+(id)alloc 
{ 
    @synchronized([ImageCache class]) 
    { 
     NSAssert(sharedImageCache == nil, @"Attempted to allocate a second instance of a singleton."); 
     sharedImageCache = [super alloc]; 

     return sharedImageCache; 
    } 

    return nil; 
} 

-(id)init 
{ 
    self = [super init]; 
    if (self != nil) 
    { 
     imgCache = [[NSCache alloc] init]; 
    } 

    return self; 
} 

- (void) AddImage:(UIImage *)image imageURL:(NSString *)imageURL 
{ 
    if (image==nil) { 

    } 
    else 
    { 
     [imgCache setObject:image forKey:imageURL]; 
    } 
} 

- (NSString*) GetImage:(NSString *)imageURL 
{ 
    return [imgCache objectForKey:imageURL]; 
} 

- (BOOL) DoesExist:(NSString *)imageURL 
{ 
    if ([imgCache objectForKey:imageURL] == nil) 
    { 
     return false; 
    } 

    return true; 
} 

@end