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

    }

  • 相关阅读:
    安卓性能优化总结
    Splash广告界面
    安卓实现版本升级
    Kotlint集合简单总结
    Kotlin在处理GET和POST请求的数据问题
    udp 局域网群聊
    java 网络编程
    关于软件工程的课程建议
    结对编程--四则运算
    简单的结对代码练习
  • 原文地址:https://www.cnblogs.com/easonoutlook/p/2642813.html
Copyright © 2011-2022 走看看