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

    }

  • 相关阅读:
    《Joint Face Detection and Alignment using Multi-task Cascaded Convolutional Networks》
    YOLO(v1)
    循环赛日程表问题(分治法)
    平面最接近点对问题(分治法)
    SQLite学习心得
    如何找到Linux下常用命令的源码
    WiFi安全之WPA介绍
    七种不良的工作习惯
    Git 技巧小结
    微信智能硬件平台 简介
  • 原文地址:https://www.cnblogs.com/easonoutlook/p/2642813.html
Copyright © 2011-2022 走看看