惟伊の恋的专栏

我那么拼命,那么努力,只是为了,让自己变得足够优秀,只是为了,能兑现,我来守护你一生一世的承诺...

IOS通过URL下载保存图片

-(void) downLoadImageFromURL:(NSString *)fileURL withName:(NSString *)imageName
{
    //Check Image Is Exists
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString * filePath = [DOCUMENT_PATH stringByAppendingPathComponent:imageName];
    filePath = [NSString stringWithFormat:@"%@.jpg", filePath];
    if ([fileManager fileExistsAtPath:filePath])
    {
        return ;
    }
    
    //Get Image From URL
    UIImage * imageFromURL = [self getImageFromURL:fileURL];
    
    //Save Image to Directory
    [self saveImage:imageFromURL withFileName:imageName ofType:@"jpg" inDirectory:DOCUMENT_PATH];
    
    //Get Document Path All Files
    NSArray *file = [[[NSFileManager alloc] init] subpathsAtPath:DOCUMENT_PATH];
    NSLog(@"%@",file);
}

-(UIImage *) getImageFromURL:(NSString *)fileURL
{
    NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:fileURL]];
    return [UIImage imageWithData:data];
}

-(void) saveImage:(UIImage *)image withFileName:(NSString *)imageName ofType:(NSString *)extension inDirectory:(NSString *)directoryPath
{
    if ([[extension lowercaseString] isEqualToString:@"png"])
    {
        [UIImagePNGRepresentation(image) writeToFile:[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", imageName, @"png"]] options:NSAtomicWrite error:nil];
    }
    else if ([[extension lowercaseString] isEqualToString:@"jpg"] || [[extension lowercaseString] isEqualToString:@"jpeg"])
    {
        [UIImageJPEGRepresentation(image, 1.0) writeToFile:[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", imageName, @"jpg"]] options:NSAtomicWrite error:nil];
    }
    else
    {
        NSLog(@"文件后缀不认识");
    }
}

调用方法

NSString * imageURL = @"http://img.sccnn.com/bimg/337/49760.jpg";
    [self downLoadImageFromURL:imageURL withName:@"image"];


demo下载地址:http://download.csdn.net/detail/linyang24/9283211

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/linyang24/article/details/49930125
个人分类: IOS开发
上一篇iphone屏幕尺寸
下一篇IOS获取时间戳并转化为对应格式的时间
想对作者说点什么? 我来说一句

C#通过URL保存图片到本地

2011年07月22日 2KB 下载

url图片 保存文件到本地

1

u013699800 u013699800

2016-04-21 13:57:46

阅读数:1378

没有更多推荐了,返回首页

关闭
关闭