Ios NSOperationQueue问题
我有一个代码,我在ios 6中用作图像下载器,但当我在使用ios 5的不同应用程序中使用相同的代码时,它会给我错误Ios NSOperationQueue问题,ios,iphone,ios5,nsoperationqueue,image-loading,Ios,Iphone,Ios5,Nsoperationqueue,Image Loading,我有一个代码,我在ios 6中用作图像下载器,但当我在使用ios 5的不同应用程序中使用相同的代码时,它会给我错误NSInvalidArgumentException',原因:'-[NSNull length]:发送到实例的未识别选择器是我正在使用的代码 [self.imageDownloadingQueue addOperationWithBlock:^{ NSURL *imageUrl = [NSURL URLWithString:ImageURL];
NSInvalidArgumentException',原因:'-[NSNull length]:发送到实例的未识别选择器是我正在使用的代码
[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++;
}
请尝试以下代码:
[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;
}];
}
}];
希望这会对您有所帮助。您一定要检查发送到操作中的图像URL
。看起来,这个ImageURL
是[NSNull-null]
。
如果它是一个局部变量,是否声明为\u block NSString*ImageUrl
?您能确切地告诉我它在哪一行崩溃吗?抱歉,这是我得到的那一行错误NSURL*ImageUrl=[NSURL URLWithString:ImageUrl];错误为[NSNull length]:无法识别的选择器发送到实例如何使用调试器并找到问题?我尝试调试,但它没有帮助我定义函数的图像url begin,此代码在ios 6上工作正常问题是它在ios 5上不工作我这样定义它NSString*ImageURL=@“example_image.png”代码>当我使用\uu块NSString*ImageURL代码>应用程序不再崩溃,但图像链接不会根据功能更改我添加整个功能的代码什么不会更改?UIImageView
中的链接或图像?是的,我是如何更改的,但在相同的问题出现后:(首先,尝试在操作中设置断点,查看是否接收到图像数据并将其正确转换为UIImage。