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

    }

  • 相关阅读:
    c#参数传递几点小结
    c#线程初探(二)
    c#线程初探(一)
    c#:浅克隆和深克隆,序列化和反序列化
    c#冒泡、快速、选择和插入排序算法的项目应用
    c#运算符几点小结
    文件操作(无代码)
    不仅仅C#缺点(永远未完)
    《道德经》程序员版第五章
    《道德经》程序员版第四章
  • 原文地址:https://www.cnblogs.com/easonoutlook/p/2642813.html
Copyright © 2011-2022 走看看