美文网首页iOS开发
保存图片到本地和相册

保存图片到本地和相册

作者: 屮艸芔 | 来源:发表于2016-03-14 00:16 被阅读934次

    1.保存到iOS相册需要引入QuartzCore.framework框架,具体代码如下:.h文件#importUIImageView *imageView;

    @property (nonatomic, retain) IBOutlet UIImageView *imageView;

    .m文件

    UIGraphicsBeginImageContext(imageView.bounds.size);

    [imageView.layer renderInContext:UIGraphicsGetCurrentContext()];

    UIImage *temp = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    UIImageWriteToSavedPhotosAlbum(temp, nil, nil, nil);

    2.保存图片到本地

    -(UIImage *) getImageFromURL:(NSString *)fileURL {

    NSLog(@"执行图片下载函数");

    UIImage * result;

    NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:fileURL]];

    result = [UIImage imageWithData:data];

    return result;

    }

    -(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 {

    //ALog(@"Image Save Failed\nExtension: (%@) is not recognized, use (PNG/JPG)", extension);

    NSLog(@"文件后缀不认识");

    }

    }

    保存

    -(UIImage *) loadImage:(NSString *)fileName ofType:(NSString *)extension inDirectory:(NSString *)directoryPath {

    UIImage * result = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.%@", directoryPath, fileName, extension]];

    return result;

    }

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

    NSLog(@"保存路径:%@",documentsDirectoryPath);

    //Get Image From URL

    UIImage * imageFromURL = [self getImageFromURL:@"http://file.duteba.com/phone/2009/04/5/ftGxL8kcUL.jpg"];

    //Save Image to Directory

    [self saveImage:imageFromURL withFileName:@"MyImage" ofType:@"jpg" inDirectory:documentsDirectoryPath];

    //Load Image From Directory

    UIImage * imageFromWeb = [self loadImage:@"MyImage" ofType:@"jpg" inDirectory:documentsDirectoryPath];

    [img setImage:imageFromWeb];

    //取得目录下所有文件名

    NSArray *file = [[[NSFileManager alloc] init] subpathsAtPath:documentsDirectoryPath];

    //NSLog(@"%d",[file count]);

    NSLog(@"%@",file);

    显示

    NSString *documentsDirectory2 = [path2 objectAtIndex:0]; //path数组里貌似只有一个元素

    //字符串拼接得到文件完整路径

    NSString *filestr = @"/MyImage.jpg";

    NSString *newstr = [documentsDirectory2 stringByAppendingString:filestr];

    NSLog(@"完整路径是:%@",newstr);

    NSData *dd = [NSData dataWithContentsOfFile:newstr];

    [img222 setImage:[UIImage imageWithData:dd]];

    另外,还可以将图片的路径保存在plist文件里,这样可以避免过多的IO操作。

    相关文章

      网友评论

        本文标题:保存图片到本地和相册

        本文链接:https://www.haomeiwen.com/subject/pqrflttx.html