简体   繁体   English

使用NSURLConnection将图像加载到UIImageView中

[英]Loading Image into UIImageView using NSURLConnection

Hey all. 大家好。 I'm really new at this obj-c/xcode stuff. 我真的很擅长这个obj-c / xcode的东西。 I'm trying to load the background of my xib file. 我正在尝试加载我的xib文件的背景。 To do this i'm using a UIImageView and populating that with an image I found from the net. 要做到这一点,我正在使用UIImageView并使用我从网上找到的图像填充它。 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 how. 我被告知使用NSURLConnection来解决问题,但不知道如何。 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等效代码?

NSURLConnection will download the data in a new thread, so you app will feel much faster. NSURLConnection将在新线程中下载数据,因此您的应用程序会感觉更快。

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.h

AsyncImage.m 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 您需要在使用webservice时对此进行解析。就像这样

-(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. 然后在你的Viewcontroller访问数据,从解析你需要传递对象,使用数组或字典。

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

There is an example which may help: 有一个例子可能会有所帮助:

NSURLConnection loading image example NSURLConnection加载图片示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM