1

我有一个 Web 服务,我使用这行代码从 cocoa 对其进行 HTTP 调用:

NSData *imageData = [[NSData alloc] initWithContentsOfUrl:url options: NSDataReadingUncached error:&error];

从这个 URL 加载图片有时需要 10 秒,有时需要 30 秒。我尝试从普通浏览器加载此 URL,它需要 1-2 秒。我正在做延迟加载,问题是加载此 URL 的内容与普通浏览器相比所需的时间。两个测试都是在同一个网络上完成的。

下载.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。

有任何想法吗?

4

0 回答 0