UIImageView需要时间来加载图像

时间:2013-08-22 05:27:58

标签: objective-c uiimageview nsurlconnection nsoperationqueue nsinvocationoperation

我有一些问题 我想从特定链接下载来自互联网的图像 下载此图像发生在其他类中,即singlton类名称moreAppInternet.m

我的moreAppInterNet.m文件如下

// finishedImgDling是bool

// mutableData是NSMutableData

// urlConnection是NSURLConnection

// appimage是UIImage

// imageUrl是包含链接的NSString

+(ID)sharedManager {

static moreAppInterNet *sharedMyManager = nil;

@synchronized(self) {

    if (sharedMyManager == nil)

        sharedMyManager = [[self alloc] init];
}

return sharedMyManager;

}

- (无效)downloadImageFromUrl {
    finishedImgDling = NO;

[self.mutableData setLength:0];

self.urlConnection = [NSURLConnection connectionWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.imageUrl]] delegate:self];


while(!finishedImgDling) {
    [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
}   

}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {

if (connection == self.urlConnection)
{

    NSLog(@"imageDling");
}

}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {

if (connection == self.urlConnection)
{

    if (self.mutableData == nil)
    {
        self.mutableData = [NSMutableData data];
    }

    [self.mutableData appendData:data];
}

}

- (void)connectionDidFinishLoading:(NSURLConnection *)连接 {

if (connection == self.urlConnection)
{
    NSLog(@"image Downloaded");

    finishedImgDling = YES;

    [self setUrlConnection:nil];

    [self storeImage];
}

}

- (无效)storeImage {

[self setAppimage:nil];

self.appimage = [UIImage imageWithData:self.mutableData];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];     
NSFileManager *fileManager = [NSFileManager defaultManager];

NSString *path =[documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"appImage.png"]];

[fileManager createFileAtPath:path contents:UIImagePNGRepresentation(self.appimage) attributes:nil];

 NSLog(@"downloading and storing complete");

}

我的另一个ViewController如下所示,我称之为该函数

- (无效)viewDidLoad中 {

[moreAppInterNet sharedManager];

[self downloadImage];

}

- (无效)downloadImage {

 NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(download_Image_from_other_class) object:nil];

NSOperationQueue *que = [[NSOperationQueue alloc] init];

[que addObserver:self forKeyPath:@"operations" options:0 context:NULL];

[que addOperation:operation];

[operation release];

self.dlingQue = que;

[que release];

}

- (无效)download_Image_from_other_class {

[[moreAppInterNet sharedManager] downloadImageFromUrl];

}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object                          更改:(NSDictionary *)更改上下文:(void *)上下文 {

if ([keyPath isEqualToString:@"operations"] && object == self.dlingQue)
{

    if ([self.dlingQue operationCount] == 0)
    {

        self.imgView = [[moreAppInterNet sharedManager] appimage];

        //it takes lots of time to display image in UIImageView
        //Even though Above line get executed
    }
}
else {

    [super observeValueForKeyPath:keyPath ofObject:object
                           change:change context:context];
}

}

问题是 self.imgView需要很多时间来加载图像大约5秒,即使它已完全下载

任何人都可以帮助我或告诉我我做错了什么

2 个答案:

答案 0 :(得分:1)

我用于网络的库是AFNetworking

答案 1 :(得分:0)

我认为您的代码会在您请求时再次下载图片。

您需要检查downloadImageFromUrl中是否包含该文件。

可能您需要更强大的库来处理文件下载。