Loading an image into a UIImageView using NSURLConnection

Hey there. I am really new to this obj-c / xcode. I am trying to download the background of my xib file. For this, I use UIImageView and fill it with the image I found from the web. the problem is that it REALLY slows down. It feels like it is falling, but it is not. I was asked to use NSURLConnection to fix the problem, but I don't know how to do it. Here I used the code earlier.

wallpaper.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://mysite.com/mobile/wallpaperR.asp?id=%i",customerID]]]];

How to translate the above code into the equivalent of NSURLConnection?

+3
source share
3 answers

NSURLConnection will load data into a new stream, so the application will be much faster.

, .

, , , , :

AsyncImage.h

AsyncImage.m

, , , , , .

+6

, webservice.Like this

-(void)startParsingForPhotoAlbumList:(NSString*)providerIdString
{
    NSString *urlString = [NSString stringWithFormat:@"http://YourUrl/showalbumxml.php?id=%@&show=show",providerIdString];
    NSURL *xmlURL = [NSURL URLWithString:urlString];
    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] initWithURL:xmlURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0]autorelease];
    NSURLResponse *returnedResponse = nil;
    NSError *returnedError = nil;
    NSData *itemData  = [NSURLConnection sendSynchronousRequest:request returningResponse:&returnedResponse error:&returnedError];
    self.xmlParser = [[NSXMLParser alloc] initWithData:itemData];       
    [xmlParser setDelegate:self];
    [xmlParser parse];
}

.

- (void)parserDidEndDocument:(NSXMLParser *)parser
{
    if ([[[resultArray objectAtIndex:1]objectForKey:@"Transaction"]isEqualToString:@"Myapp/snaps"]) 
    {
        [(LoginViewController *)obj getRegisterResult:resultArray];
    }
}

Viewcontroller , , .

NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:itemImagePath]];
UIImage *image = [[UIImage alloc] initWithData:imageData];
+1

Source: https://habr.com/ru/post/1793765/


All Articles