0

我在 uiimageview 中添加了一个“正在加载”视图以指示正在加载图像,然后在异步调度上我正在为图像充电并在完成后删除“正在加载”视图,问题是如果我调用两次此方法“加载”视图被添加了两次,第二个永远不会被删除。

编辑:

正在查看.h

UIView *loading;

正在查看.m

loading = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
loading.backgroundColor = [UIColor blackColor];
loading.alpha = 0.8;
loading.layer.cornerRadius = 10;
loading.center = CGPointMake(imageView.frame.size.width/2, imageView.frame.size.height/2);

if (![imageView.subviews containsObject:loading]) {
    [imageView addSubview:loading];
}
dispatch_queue_t downloadFoto = dispatch_queue_create("Get Photo", NULL);
dispatch_async(downloadFoto, ^{
    UIImage *image = [[UIImage alloc]initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[selectedImage objectForKey:@"url"]]]];

    dispatch_sync(dispatch_get_main_queue(), ^{
        if (image) {
            [imageView setImage:image];
            [imageView setNeedsLayout];
            if ([imageView.subviews containsObject:loading]) {
                [loading removeFromSuperview];
            }
        }
    });
});

如果只调用一次,或者如果我在加载已经删除之后调用它一切正常,问题是如果我在块完成之前调用它。

谢谢大家,这是我最后的解决方案。

if (!blackView) {
UIActivityIndicatorView *load = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
blackView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
blackView.backgroundColor = [UIColor blackColor];
blackView.alpha = 0.8;
blackView.layer.cornerRadius = 10;
blackView.center = CGPointMake(fotoVisual.frame.size.width/2, fotoVisual.frame.size.height/2);
load.center = CGPointMake(blackView.frame.size.width/2, blackView.frame.size.height/2);
[load startAnimating];
[blackView addSubview:load];


[fotoVisual setImage:[UIImage imageNamed:@"previewImagen.png"]];
descripcionFotoView.text = [selectedImage objectForKey:@"titulo"];

}
if (![fotoVisual.subviews containsObject:blackView]) {
    [fotoVisual addSubview:blackView];
}
dispatch_queue_t downloadFoto = dispatch_queue_create("Get Photo", NULL);
dispatch_async(downloadFoto, ^{
    [fotoVisual setImageWithURL:[NSURL URLWithString:[selectedImage objectForKey:@"url"]]
               placeholderImage:[UIImage imageNamed:@"previewImagen.png"]];
    dispatch_async(dispatch_get_main_queue(), ^{
            [blackView removeFromSuperview];
            blackView = nil;
    });
});
dispatch_release(downloadFoto);
4

2 回答 2

2

看起来您遇到的问题是您loading每次都在创建一个新视图,这不是您想要的。当你每次都有新的看法时,你[imageView.subviews containsObject:loading]永远不会是真的。loading

您可以更改创建逻辑以进行这样的检查

if (!loading) {
  // configure loading
  [imageView addSubview:loading];
}

然后将您的电话修改为类似

dispatch_sync(dispatch_get_main_queue(), ^{
  if (image) {
    [imageView setImage:image];
    [imageView setNeedsLayout];
    [loading removeFromSuperview];
    loading = nil;
  }
});

您使用dispatch_sync而不是有什么具体原因dispatch_async吗?

于 2014-05-26T19:56:58.603 回答
1

在块中添加您loading的子视图dispatch_async

dispatch_queue_t downloadFoto = dispatch_queue_create("Get Photo", NULL);
dispatch_async(downloadFoto, ^{

if (!loading.superview) {
// we assume your loading view doesn't have a superview so we can add it to imageView
   dispatch_sync(dispatch_get_main_queue(), ^{        
     [imageView addSubview:loading];
  };
}

    UIImage *image = [[UIImage alloc]initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[selectedImage objectForKey:@"url"]]]];


        if (image) {
          dispatch_sync(dispatch_get_main_queue(), ^{
            [imageView setImage:image];
            [imageView setNeedsLayout];
            if ([imageView.subviews containsObject:loading]) {
                [loading removeFromSuperview];
            }
          });
        }
});

对于异步图像下载,您也可以使用SDWebimage 只需下载项目并将 SDWebimage 文件夹添加到您的项目并按如下方式使用

#import "UIImageView+WebCache.h"

[self.imageView setImageWithURL:[NSURL URLWithString:[selectedImage objectForKey:@"url"]]
                   placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

这也将缓存图像。

于 2014-05-26T19:33:37.507 回答