0

我有这种方法,我用它来加载许多图像以滚动视图,但是当从 url 加载图像时,我的滚动视图被卡住了,我无法理解如何在后台加载它们,所以用户不会感觉到它。

此方法将在“for”循环中调用几次 (8)。

- (void)loadPhotosToLeftscroll{

    //getting image information from JSON
    NSMutableDictionary *photoDict;
    photoDict = [leftPhotoArray lastObject];

    //getting the photo
    NSString *photoPath = [photoDict objectForKey:@"photos_path"];
    NSLog(@"photo Path:%@",photoPath);


    NSData * imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:photoPath]];    

            UIImage *image = [UIImage imageWithData:imageData];
            // use the image how you like, say, as your button background

            //calculating the hight of next photo
            UIImageView *leftImage = [leftBlockScroll.subviews lastObject];

            //allocating photoView
            UIImageView *photoView = [[UIImageView alloc]initWithFrame:CGRectMake(5 , leftImage.frame.origin.y + leftImage.frame.size.height+5, image.size.width/2, image.size.height/2 )];
            photoView.userInteractionEnabled=YES;
            [photoView.layer setMasksToBounds:YES];
            [photoView.layer setCornerRadius:3];


            //getting items list
            NSDictionary *sh_items = [photoDict objectForKey:@"items"];

            //adding image button
            UIButton *imageOverButton = [UIButton buttonWithType:UIButtonTypeCustom];
            imageOverButton.frame = CGRectMake(0, 0, photoView.frame.size.width, photoView.frame.size.height);
            [imageOverButton addTarget:self action:@selector(LeftimagePressed:) forControlEvents:UIControlEventTouchUpInside];
            [imageOverButton setTag:[leftPhotoArray count]-1];
            [photoView addSubview:imageOverButton];

            //adding sh button to imageView
            [self addThe_sh_signs:sh_items To_ImageView:photoView];

            //subViewing the image to the scrollView
            [self insert_Image:image toImageView:photoView in_Scroll:leftBlockScroll];

            //calclulating the position of next imageView in scroll.
            nextLeftPhotoHight = photoView.frame.size.height + photoView.frame.origin.y + 5;

            //calculating the hight of the highest scroll view in both.
            leftBlockScroll.contentSize = CGSizeMake(160, [self theSizeOfScrollViewHight]);
            rightBlocScroll.contentSize = CGSizeMake(160, [self theSizeOfScrollViewHight]);
            isLoadindContant = NO;
            [self.view reloadInputViews];
            [leftBlockScroll reloadInputViews];

}

请不要将我发送到一些试图解释如何使用异步的链接。试着按照你在这里看到的方法来解释。 我在这里有任何问题,你需要帮助我。

4

1 回答 1

2

您必须以适当的方式异步执行此操作。我不认为有任何解决办法。我子类化了一个 UIImageView 对象,并将它的许多实例放置在 talbe 的单元格中(在您的情况下,在滚动视图中)。子类对象使用 url 初始化并异步加载它们的图像(有一些缓存,因此不会每次都加载图像)。

本教程在一开始对我帮助很大:http: //www.markj.net/iphone-asynchronous-table-image/

您只需将其应用于您的滚动视图。基本原理保持不变。

于 2012-06-27T16:00:22.987 回答