我有一个Web服务,我使用这行代码从cocoa进行HTTP调用:
NSData *imageData = [[NSData alloc] initWithContentsOfUrl:url options: NSDataReadingUncached error:&error];
有时需要10秒,有时30秒才能从此网址加载图片。 我尝试从普通浏览器加载此URL,需要1-2秒。 我正在进行延迟加载,问题是加载此URL的内容与普通浏览器所需的时间。两个测试都是在同一个网络上完成的。
Download.m
NSError *error;
NSData *imageData = [[NSData alloc] initWithContentsOfURL:self.photoRecord.URL options:NSDataReadingUncached error:&error];
NSMutableArray *jsonArray = [NSJSONSerialization JSONObjectWithData:imageData options:NSJSONReadingAllowFragments error:&myError];
NSMutableDictionary *pictureInfo = [jsonArray objectAtIndex:0];
NSString *picture;
picture = [pictureInfo valueForKey:@"Picture"];
NSData *base64Data = [[NSData alloc]initWithBase64Encoding:picture];
if (base64Data) {
UIImage *downloadedImage = [UIImage imageWithData:base64Data];
self.photoRecord.image = downloadedImage;
}
[(NSObject *)self.delegate performSelectorOnMainThread:@selector(imageDownloaderDidFinish:) withObject:self waitUntilDone:NO];
然后在主线程PictureController.m上:
-(void) loadScrollViewWithPage:(NSUInteger)page{
....
NSIndexPath *myIndexPath = [NSIndexPath indexPathForRow:page inSection:0];
[self startOperationsForPhotoRecord:aRecord atIndexPath:myIndexPath];
}
- (void)startOperationsForPhotoRecord:(PictureRecord *)record atIndexPath:(NSIndexPath *)indexPath {
Download *imageD = [[Download alloc] initWithPhotoRecord:record atIndexPath:indexPath delegate:self];
[self.pendingOperations.downloadsInProgress setObject:imageD forKey:indexPath];
[self.pendingOperations.downloadQueue addOperation:imageD];
}
然后我有委托方法在下载完成后更新UIImageView,工作正常。
正在加载的内容大小约为400kb。
有什么想法吗?