【问题标题】:NSOperationQueue IssueNSOperationQueue 问题
【发布时间】:2013-12-17 06:02:42
【问题描述】:

我有一个代码,我在 ios 6 中用作图像下载器,但是当我在使用 ios 5 的不同应用程序中使用相同的代码时,它给了我错误 NSInvalidArgumentException', reason: '-[NSNull length]: unrecognized selector sent to instance 听到的是我正在使用的代码

[self.imageDownloadingQueue addOperationWithBlock:^{
            NSURL *imageUrl   = [NSURL URLWithString:ImageURL];
            NSData *imageData = [NSData dataWithContentsOfURL:imageUrl];
            UIImage *images    = nil;
            if (imageData)
                images = [UIImage imageWithData:imageData];
            if (images){
                [self.imageCache setObject:images forKey:ImageURL];
                [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                    // make sure the cell is still visible
                    imageV.image = images;
                }];
            }
        }];

谁能告诉我我做错了什么,因为这段代码在单独下载图像时对我有很大帮助。谢谢你

PS 我正在使用的整个函数

-(void) setScrollView:(UIScrollView *)scrollview :(NSArray *)contentArray{

    int x = 0;

    int i = 0;

    NSString *ImageURL=@"http://www.online-image-editor.com/styles/2013/images/example_image.png";



    for(NSDictionary *str in contentArray){

        UIImageView *imageV = [[UIImageView alloc ] initWithFrame:CGRectMake(x, 0, 200,125)];
        @try {
            ImageURL = [str objectForKey:@"image"];
       }
        @catch (NSException * e) {
            NSLog(@"Exception: %@", [e description]);
        }


        imageV.userInteractionEnabled = YES;

        ImageTapGesture *singleTap = [[ImageTapGesture alloc] initWithTarget:self action:@selector(tapDetectedMain:)];
        singleTap.newItem = str;
        singleTap.currentItem = i ;
        singleTap.numberOfTapsRequired = 1;

        [imageV addGestureRecognizer:singleTap];

       UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(x,125,200,20)];

        [title setFont:[UIFont systemFontOfSize:11]];
        title.numberOfLines = 2;
       title.autoresizingMask = UIViewAutoresizingFlexibleHeight;
       [title setBackgroundColor:[UIColor blackColor]];
       [title setTextColor:[UIColor whiteColor]];
       title.text = [NSString stringWithFormat:@" %@",[str objectForKey:@"title"]];


       x += title.frame.size.width+2;

        UIImage *cachedImage = [self.imageCache objectForKey:ImageURL];

       if (cachedImage){
           imageV.image = cachedImage;
        }
        else{
            imageV.image = [UIImage imageNamed:@"vdo.png"];

           [self.imageDownloadingQueue addOperationWithBlock:^{
               NSURL *imageUrl   = [NSURL URLWithString:ImageURL];
                NSData *imageData = [NSData dataWithContentsOfURL:imageUrl];
                UIImage *images    = nil;
                if (imageData)
                    images = [UIImage imageWithData:imageData];
                  if (images){
                      [self.imageCache setObject:images forKey:ImageURL];
                      [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                        // make sure the cell is still visible
                        imageV.image = images;
                   }];
                }
            }];

        }

        [scrollview addSubview:imageV];
        [scrollview addSubview:title];
    }

    scrollview.contentSize = CGSizeMake(x, scrollview.frame.size.height);
    i++;

}

【问题讨论】:

  • 你能准确说出它是在哪一行崩溃的吗?
  • 对不起,这是我收到错误 NSURL *imageUrl = [NSURL URLWithString:ImageURL];错误是 [NSNull length]: unrecognized selector sent to instance
  • 使用调试器定位问题如何?
  • 我尝试调试但没有帮助

标签: ios iphone ios5 nsoperationqueue image-loading


【解决方案1】:

您绝对应该检查您发送到操作中的ImageURL。看来,这个ImageURL 是[NSNull null]。 如果是局部变量,是否声明为__block NSString *ImageUrl?

【讨论】:

  • 我定义了我的函数的图像 url 开始,这段代码在 ios 6 问题上工作正常是它在 ios 5 中不起作用我这样定义它NSString *ImageURL=@"example_image.png";
  • 当我使用__block NSString *ImageURL; 应用程序不再崩溃但图像链接没有根据功能改变我添加我的整个功能的代码
  • 什么没有改变? UIImageView中的链接或图片?
  • 是的,我如何设法改变这一点,但同样的问题又回来了:(
  • 首先尝试在操作内部设置断点,看看是否接收到图片数据并正确转换为UIImage。
【解决方案2】:

请尝试以下代码:

[self.imageDownloadingQueue addOperationWithBlock:^{
    NSURL *imageUrl ;
    if((ImageURL!=nil)&&(![ImageURL isKindOfClass:[NSNull class]]))
    {
        imageUrl= [NSURL URLWithString:ImageURL];
    }
    NSData *imageData;
    if((imageUrl!=nil)&&(![imageUrl isKindOfClass:[NSNull class]]))
    {
        imageData = [NSData dataWithContentsOfURL:imageUrl];
    }

    UIImage *images    = nil;
    if((imageData!=nil)&&(![imageData isKindOfClass:[NSNull class]]))
        images = [UIImage imageWithData:imageData];
    if((images!=nil)&&(![images isKindOfClass:[NSNull class]]))
    {
        [self.imageCache setObject:images forKey:ImageURL];
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            // make sure the cell is still visible
            imageV.image = images;
        }];
    }
}];

希望这会对你有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-05
    相关资源
    最近更新 更多