开发者

Loading Image into UIImageView using NSURLConnection

开发者 https://www.devze.com 2023-02-12 06:11 出处:网络
Hey all. I\'m really new at this obj-c/xcode stuff. I\'m trying to load the background of my xib file. To do this i\'m using a UIImageView and populating that with an image I found from the net. the p

Hey all. I'm really new at this obj-c/xcode stuff. I'm trying to load the background of my xib file. To do this i'm using a UIImageView and populating that with an image I found from the net. the problem with that is that it's REALLY slow. Feels like it's crashing but it's not. I was told to use an NSURLConnection to fix the problem but don't know ho开发者_Python百科w. Here's the code i was using previously.

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

How do i translate the above code into the NSURLConnection equivalent?


NSURLConnection will download the data in a new thread, so you app will feel much faster.

It is pretty easy to implement but it does require you to understand the concept of delegation.

I have a useful class I created to handle image downloads on separate threads, I will share the code with you, with comments to let you know what is going on:

AsyncImage.h

AsyncImage.m

If you need help implementing it just leave a comment and I can help you get it working, I remember this used to be pain for me too when I started developing.


You need to do parsing for this as you are using the 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];
}

and need to implement parser's delegate method as an example.

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

Then in your Viewcontroller access the data,from parsing you need to pass objects,using array or dictionary.

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


There is an example which may help:

NSURLConnection loading image example

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号