从在线加载图像时的 UIActivityIndi​​catorView ?

发布于 2024-10-21 19:10:49 字数 465 浏览 2 评论 0原文

我有一个简单的脚本,显示在来自互联网的 UIImageView 中。

有没有办法显示 UIActivityIndi​​catorView 或类似的东西来向用户显示它正在加载?

这是我的最小代码:

NSString *imagePath = [NSString stringWithFormat:@"urlofimagehere.jpg"];
NSData *mydata = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:imagePath]];
UIImage *myimage = [[UIImage alloc] initWithData:mydata];
[imageView setImage:myimage];
[myimage release];

谢谢! 库尔顿

编辑:代码已删除

I have a simple script that displays in a UIImageView from the internet.

Is there a way to display a UIActivityIndicatorView or something of that sort to show the user that it's loading?

Here's my minimal code:

NSString *imagePath = [NSString stringWithFormat:@"urlofimagehere.jpg"];
NSData *mydata = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:imagePath]];
UIImage *myimage = [[UIImage alloc] initWithData:mydata];
[imageView setImage:myimage];
[myimage release];

Thanks!
Coulton

EDIT: CODE REMOVED

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

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

发布评论

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

评论(1

温柔嚣张 2024-10-28 19:10:49
-[NSData initWithContentsOfURL:]

是一个同步方法。这意味着它将阻塞,直到数据通过网络加载。如果您希望能够保持 UI 响应能力,您将需要使用异步方法。有关更多信息,请阅读:http://akosma.com/2010 /05/28/initwithcontentsofurl-methods-considered-harmful/

我建议使用 NSURLRequest 和 NSURLRequestDelegate 在数据加载完成时接收回调。这是一个例子:

- (void)loadImageAtURL(NSString *)urlString {
    [myUIActivityIndicatorView startAnimating];

    // SHOW NETWORK INDICATOR
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

    // SET UP THE REQUEST
    NSURL *url = [[NSURL alloc] initWithString:urlString];
    NSURLRequest *request = [[[NSURLRequest alloc] initWithURL:url] autorelease];

    // SET UP THE CONNECTION
    connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    mutData = [[[NSMutableData alloc] init] retain];
}

- (void)connection:(NSURLConnection *)connection
  didFailWithError:(NSError *)error {
    // Handle error
}

- (void)connection:(NSURLConnection *)connection
    didReceiveData:(NSData *)data {
    [mutData appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)aConnection {

    // ONCE LOADED HIDE NETWORK INDICATOR
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

    // RELEASE THE CONNECTION
    [connection release];

    // CREATE NEW UIIMAGE FROM THE DATA
    image = [[[UIImage alloc] initWithData:mutData] retain];
    [mutData release];

    // HIDE THE ACTIVITY INDICATOR
    // Make sure hidesWhenStopped is set to YES on the activity indicator.
    [myUIActivityIndicatorView stopAnimating];
    [myUIImageView setImage:image];
    [image release];
}

希望这有帮助!

-[NSData initWithContentsOfURL:]

is a synchronous method. This means it will block until the data is loaded over the network. If you want to be able to keep your UI responsive, you'll want to use an asynchronous approach. For more info, read this: http://akosma.com/2010/05/28/initwithcontentsofurl-methods-considered-harmful/

I'd recommend using NSURLRequest with an NSURLRequestDelegate to receive callbacks when the data is finished loading. Here's an example:

- (void)loadImageAtURL(NSString *)urlString {
    [myUIActivityIndicatorView startAnimating];

    // SHOW NETWORK INDICATOR
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

    // SET UP THE REQUEST
    NSURL *url = [[NSURL alloc] initWithString:urlString];
    NSURLRequest *request = [[[NSURLRequest alloc] initWithURL:url] autorelease];

    // SET UP THE CONNECTION
    connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    mutData = [[[NSMutableData alloc] init] retain];
}

- (void)connection:(NSURLConnection *)connection
  didFailWithError:(NSError *)error {
    // Handle error
}

- (void)connection:(NSURLConnection *)connection
    didReceiveData:(NSData *)data {
    [mutData appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)aConnection {

    // ONCE LOADED HIDE NETWORK INDICATOR
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

    // RELEASE THE CONNECTION
    [connection release];

    // CREATE NEW UIIMAGE FROM THE DATA
    image = [[[UIImage alloc] initWithData:mutData] retain];
    [mutData release];

    // HIDE THE ACTIVITY INDICATOR
    // Make sure hidesWhenStopped is set to YES on the activity indicator.
    [myUIActivityIndicatorView stopAnimating];
    [myUIImageView setImage:image];
    [image release];
}

Hope this helps!

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