일상+

Xcode 비동기 방식 본문

iPhone

Xcode 비동기 방식

이종준 2015. 9. 21. 17:20

 dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);

    dispatch_async(queue, ^(void){

        NSString *imagePath =[NSString stringWithFormat:POI_THUMBNAIL_IMAGE_PATH, pois.photoId];

        NSURL * imageURL = [NSURL URLWithString:imagePath];

        NSData * imageData = [NSData dataWithContentsOfURL:imageURL];

        UIImage * image = [UIImage imageWithData:imageData scale:4];

        dispatch_async(dispatch_get_main_queue(), ^(void){

            //비동기로 셀이 호출 되므로 해당 셀에만 이미지 작업이 되도록 설정

            if(cell.tag == indexPath.row) {

                float cellImageY = (cell.bounds.size.height - image.size.height) / 2;

                float cellImageX = cellImageY - 10;

                UIImageView *thumbImage = [[UIImageView alloc]initWithFrame:CGRectMake(cellImageX, cellImageY, image.size.width, image.size.height)];

                thumbImage.image = image;

                [cell addSubview:thumbImage];

            }

        });

    });



Comments