首先感谢您的帮助。
我正在尝试使用AFNetworking下载多个图像,问题是当我尝试在块成功部分中添加手势识别器时,在完成后我收到错误。
这是我的代码。
- (void)loadPictures {
NSArray *listItems = [[_dictForData objectForKey:@"imgs"] componentsSeparatedByString:ITEM_SEPARATOR];
NSLog(@" == Num Images %d", [listItems count]-1);
NSMutableArray *imgNames= [[NSMutableArray alloc] init];
for(int i=0;i<[listItems count]-1;i++)
{
NSString *nameImage = [listItems[i] componentsSeparatedByString:TAG_SEPARATOR][1];
NSLog(@" == Name Image %@", nameImage);
[imgNames addObject: nameImage];
NSLog(@"== %@", [imgNames objectAtIndex:i]);
}
// Setup the array of UIImageViews
NSMutableArray *imgArray = [[NSMutableArray alloc] init];
for(NSString *name in imgNames) {
NSString *string = [NSString stringWithFormat:@"%@%@%@%@%@", SERVER, URL_FILES,[_dictForData objectForKey:@"id"],URL_IMAGES_HI,name];
NSURL *url = [NSURL URLWithString:string];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
AFHTTPRequestOperation *requestOperation = [[AFHTTPRequestOperation alloc] initWithRequest:urlRequest];
requestOperation.responseSerializer = [AFImageResponseSerializer serializer];
[requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Response: %@", responseObject);
UIImageView *tempImageView;
tempImageView = [[UIImageView alloc] init];
tempImageView.contentMode = UIViewContentModeScaleAspectFit;
tempImageView.image = responseObject;
[imgArray addObject:tempImageView];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapDetected)];
singleTap.numberOfTapsRequired = 1;
[tempImageView setUserInteractionEnabled:YES];
[tempImageView addGestureRecognizer:singleTap];
if ([imgArray count] == [listItems count]-1) {
//CGSize pageSize = self.scrollView.frame.size; // scrollView is an IBOutlet for our UIScrollView
NSUInteger page = 0;
NSUInteger widthPicture = 100;
NSUInteger heightPicture = 100;
for(UIView *view in imgArray) {
[self.scrollView addSubview:view];
// This is the important line
//view.frame = CGRectMake(pageSize.width * page++ + 10, 0, pageSize.width - 20, pageSize.height);
view.frame = CGRectMake(widthPicture * page++, 0, widthPicture - 5, heightPicture);
// We're making use of the scrollView's frame size (pageSize) so we need to;
// +10 to left offset of image pos (1/2 the gap)
// -20 for UIImageView's width (to leave 10 gap at left and right)
}
//self.scrollView.contentSize = CGSizeMake(pageSize.width * [imgArray count], pageSize.height);
self.scrollView.contentSize = CGSizeMake((widthPicture + 5) * [imgArray count], 100);
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Image error: %@", error);
}];
[requestOperation start];
}
}
-(void)tapDetected{
NSLog(@"single Tap on imageview");
}
编辑:
当我点按图片视图时,错误是EXC_BAD_ACCESS(代码= 1,地址= 0x7000000C)。
我需要点击创建的图片,然后在imageview上显示单击。
我做错了什么?或者我该如何解决?。
提前致谢并抱歉我的Inglish
答案 0 :(得分:0)
而不是将UIImageView添加到imgArray [imgArray addObject:tempImageView];
直接添加[self.scrollView addSubview:tempImageView];
从循环中评论它。我完全理解你正在使用CGRectMake
的循环。
仍在测试您的代码及其完全正常工作
我删除了这一行if ([imgArray count] == [listItems count]-1) {
UIImageView *tempImageView;
tempImageView = [[UIImageView alloc] init];
tempImageView.contentMode = UIViewContentModeScaleAspectFit;
tempImageView.image = responseObject;
[imgArray addObject:tempImageView];
[self.view addSubview:tempImageView];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapDetected)];
singleTap.numberOfTapsRequired = 1;
[tempImageView setUserInteractionEnabled:YES];
[tempImageView addGestureRecognizer:singleTap];