使用 NSURLConnection 将图像加载到 UIImageView 中

发布于 2024-10-18 15:19:27 字数 414 浏览 1 评论 0原文

嘿大家。我对 obj-c/xcode 的东西真的很陌生。我正在尝试加载 xib 文件的背景。为此,我使用 UIImageView 并用我从网上找到的图像填充它。问题是它真的很慢。感觉像是崩溃了,但事实并非如此。有人告诉我使用 NSURLConnection 来解决问题,但不知道如何解决。这是我之前使用的代码。

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

我如何将上面的代码转换为 NSURLConnection 等效项?

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 how. 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

兔姬 2024-10-25 15:19:27

NSURLConnection 将在新线程中下载数据,因此您的应用程序会感觉更快。

它很容易实现,但它确实需要您理解委托的概念。

我创建了一个有用的类来处理单独线程上的图像下载,我将与您共享代码,并添加注释让您知道发生了什么:

AsyncImage.h

AsyncImage.m

如果您需要帮助实现它,请发表评论我可以帮助你让它发挥作用,我记得当我开始开发时这对我来说也很痛苦。

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.

韵柒 2024-10-25 15:19:27

您需要在使用 Web 服务时对此进行解析。像这样,

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

然后在你的视图控制器中访问数据,从解析中你需要传递对象,使用数组或字典。

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

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];
橘虞初梦 2024-10-25 15:19:27

有一个示例可能会有所帮助:

NSURLConnection 加载图像示例

There is an example which may help:

NSURLConnection loading image example

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文