我有一个UItableView,分别列出出口数量,每个出口都有一个标志图像。 我在iPhone上本地保存这些图像,因此如果在iPhone上找到任何图像,它将从那里获取,如果没有,那么它将发送服务调用并获取数据。 它正在相应地工作,但是当我向下滚动桌面视图时,它会在下载图像的某个时刻被挂起,因为它被下载后再次正常工作。 是否有任何解决方案可以执行下载图像的多任务处理,并通过服务调用或在本地获取它们来表示它们。

这是我的代码..

//在本地下载插座的图像

    NSLog(@"Downloading...");
    NSString *imageLink = [NSString stringWithFormat:@"http://cf.abc.pk/outlets/l/%@",outs.logo];
    UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageLink]]];

    NSLog(@"%f,%f",image.size.width,image.size.height);
    NSString *docDir =[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSLog(@"%@",docDir);
    NSLog(@"saving png");
    NSString *pngFilePath = [NSString stringWithFormat:@"%@/%d.png",docDir,[[arrOutletIds objectAtIndex:webserviceCall] intValue]];

    BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:pngFilePath];
    if(!fileExists)
    {
        NSData *data1 = [NSData dataWithData:UIImagePNGRepresentation(image)];
        [data1 writeToFile:pngFilePath atomically:YES];
    }

===============>>#1 票数: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
                }); 
               });

希望这对你有所帮助。

===============>>#2 票数: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];
    });
}

===============>>#3 票数: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

  ask by user3422986 translate from so

未解决问题?本站智能推荐: