Objective c 从url在后台加载多少照片(异步)

Objective c 从url在后台加载多少照片(异步),objective-c,asynchronous,uiimageview,nsurlconnection,nsurl,Objective C,Asynchronous,Uiimageview,Nsurlconnection,Nsurl,我用这个方法加载很多图片到滚动视图,但是当从url加载图片时,我的滚动视图被卡住了,我不知道如何在后台加载,所以用户不会感觉到 此方法将在“for”循环中调用几次(8)。 - (void)loadPhotosToLeftscroll{ //getting image information from JSON NSMutableDictionary *photoDict; photoDict = [leftPhotoArray lastObject]; //g

我用这个方法加载很多图片到滚动视图,但是当从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];

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

您必须以适当的方式异步执行。我认为这是没有办法的。我对UIImageView对象进行了子类化,并将它的许多实例放在talbe的单元格中(在您的例子中是在滚动视图中)。子类对象使用url初始化,并异步加载它们的图像(使用一些缓存,这样图像就不会每次都加载)

- (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];

}
本教程在一开始对我帮助很大:


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

我很确定你这样做的方式解释了“前台”加载。