lianlianbushell 2012-10-31 03:13 采纳率: 0%
浏览 2752
已采纳

把网页的图片保存到iphone应用作为背景,速度太慢了

我在网上找到了把网页的图片保存到应用作为背景的方法,就是速度太慢了。

10kb的图片如果一张还可以,如果有15-20张,就像卡住了似的。有没有什么方法可以让从网上直接下载图片速度快一些?图片是批量下载的。而且还能保持图片质量。

#define kBgQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) //1

#import "downloadImage.h"

@interface downloadImage ()

@end

@implementation downloadImage

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {

    }
    return self;
}



- (void)viewDidLoad
{
    [super viewDidLoad];

    NSString *imgUrl = @"http://somesite.com/someimage.jpg";

    dispatch_async(kBgQueue, ^{

        [self performSelectorOnMainThread:@selector(downloadImageFromWeb:) withObject:imgUrl waitUntilDone:YES];

    });

}

-(void)downloadImageFromWeb:(NSString *)imgUrl{


    UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imgUrl]]];

    NSArray *parts = [imgUrl componentsSeparatedByString:@"/"];

    NSString *imgFilename = [parts lastObject];

    [self saveImage:image:imgFilename];

}


- (void)saveImage:(UIImage*)image:(NSString*)imageName {

    NSData *imageData = UIImageJPEGRepresentation(image, 100);
    NSFileManager *fileManager = [NSFileManager defaultManager];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:
                          imageName];

    [fileManager createFileAtPath:fullPath contents:imageData attributes:nil];



}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];

}

@end
  • 写回答

2条回答

  • Mr_me 2012-10-31 06:34
    关注
    • 你先检查一下电脑上的浏览器下载图片的时间多少。有可能是图片服务器端的问题呢

    • 然后试一些提高的代码,比如AsyncImageView,

    • 删除存储然后再次测试,看看是不是快了一点。

    • 你干嘛要在主线程下载?你应该在background线程下载,然后在主线程更新。你现在的方法会阻塞主线程。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料