zoukankan      html  css  js  c++  java
  • 使用GCD异步下载图片,优化

    本来在下载完成之后,在UIImageWriteToSavedPhotosAlbum 里面响应

    image: (UIImage *) image didFinishSavingWithError: (NSError *) error  contextInfo: (void *) contextInfo

    这个函数,然后来更新UI的,但是操作比较繁琐,而且UI的更新总是无法同步。所以采用GCD的方式,来完成,非常顺畅。

     

     

    - (void)downloadImageToPhotoAlbum

    {

        [self.viewaddSubview:downloadingView];

        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

            NSURL *photoUrl = [NSURL URLWithString:self.photoVersionLargerUrl];

            NSData *photoData = [NSData dataWithContentsOfURL:photoUrl];

            UIImageWriteToSavedPhotosAlbum([UIImageimageWithData:photoData], nil, nil, nil);

            dispatch_async(dispatch_get_main_queue(), ^{

                [downloadingViewremoveFromSuperview];

            

            });

        });

    }

     

    - (void)processImage:(UIImage *)image

    {

        [image retain];

        UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);

    }

     

    - (void)               image: (UIImage *) image

        didFinishSavingWithError: (NSError *) error

                     contextInfo: (void *) contextInfo

    {

    //    [downloadingView removeFromSuperview];

        NSLog(@"Save image complete!");

        if(error != nil) {

            NSLog(@"Error when saving image :%@",[error localizedDescription]);

        }

    //    [image autorelease];

    }

  • 相关阅读:
    面试总结
    回溯-递归练习题集
    2018年度业余学习总结
    12306余票高效查询--clojure实现
    Mac中wireshark如何抓取HTTPS流量?
    2017年学习总结
    Clojure——学习迷宫生成
    新生帝之2016年的计划,努力去做好!
    EmitMapper
    ASP.NET 修改地址返回图片缩率图
  • 原文地址:https://www.cnblogs.com/easonoutlook/p/2642813.html
Copyright © 2011-2022 走看看