按鈕添加網絡請求的圖片(多線程)(ios自學筆記)

    for (int i=0 ; i<ScrollViewArr.count; i++) {

        

        UIButton *button = [UIButtonbuttonWithType:UIButtonTypeCustom];

        button.frame =CGRectMake(10+i*(width +10 ),10, width, width);


        NSString *sitemCode = [[ScrollViewArrobjectAtIndex:i] objectForKey:@"sitemCode"];

        NSString *contsFileNm = [NSStringstringWithFormat:@"%@L.jpg",sitemCode];

        NSString *strUrl = [NSStringstringWithFormat:@"http://image1.oshishang.cn/%@%@", [[ScrollViewArrobjectAtIndex:i] objectForKey:@"contsPathNm"],contsFileNm];

        NSURL *URL = [NSURLURLWithString:strUrl];

 

        dispatch_queue_t xrQueue =dispatch_queue_create("loadImage",NULL);

        dispatch_async(xrQueue, ^{

            UIImage *img = [UIImageimageWithData:[NSDatadataWithContentsOfURL:URL]];

            dispatch_async(dispatch_get_main_queue(), ^{

                [button setImage:imgforState:UIControlStateNormal];

            });

        });

        [BrandScrollViewaddSubview:button];

    }

        BrandScrollView.contentSize =CGSizeMake((ScrollViewArr.count *  width)+((ScrollViewArr.count+1) * 10), 100);

}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章