slow loading using initWithContentsOfUrl

279 Views Asked by At

I have a web service and I make HTTP calls to it from cocoa using this line of code:

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

Sometimes is it take 10 seconds, sometimes 30 seconds to load the picture from this URL. I tried loading this URL from a normal browser and it takes 1-2 seconds. I'm doing lazy loading and the problem is the time it takes to load the contents of this URL vs a normal browser. Both tests were done from the same network.

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];

Then on the main thread 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];
}

Then I have the delegate method to update the UIImageView when the download is done which is working just fine.

The size of the content being loaded is around 400kb.

Any ideas?

0

There are 0 best solutions below