LoginSignup
16
16

More than 5 years have passed since last update.

GCDとBlocksを使って画像を読み込むメソッド書いてみた

Posted at

GCD使えば非同期通信系が簡単に書けるので試してみた。

-(void)loadImageGCD :(UIImageView *)iv :(CGRect)rect :(NSString *)url{
    dispatch_queue_t q_global = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_queue_t q_main = dispatch_get_main_queue();
    iv.frame = rect;

    dispatch_async(q_global, ^{
        NSString *imageUrl = url;
        UIImage *image1 = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageUrl]]];

        dispatch_async(q_main, ^{
            iv.image = image1;
        });
    });
}

これで引数のUIImageViewをaddChildとかしておけば、UIImageがよみこまれたら、そこに表示されます。

16
16
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16
16