将UIImage保存到iOS照片库和对应程序沙盒中
Posted 文化流氓
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将UIImage保存到iOS照片库和对应程序沙盒中相关的知识,希望对你有一定的参考价值。
1.保存到iOS照片库需要引入QuartzCore.framework框架,具体代码如下:
.h文件
#import <QuartzCore/QuartzCore.h>
UIImageView *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.保存到对应的沙盒目录中,具体代码如下:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"sms.gif"]]; // 保存文件的名称
BOOL result = [UIImagePNGRepresentation(imageView.image)writeToFile: filePath atomically:YES]; // 保存成功会返回YES
.h文件
#import <QuartzCore/QuartzCore.h>
UIImageView *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.保存到对应的沙盒目录中,具体代码如下:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"sms.gif"]]; // 保存文件的名称
BOOL result = [UIImagePNGRepresentation(imageView.image)writeToFile: filePath atomically:YES]; // 保存成功会返回YES
以上是关于将UIImage保存到iOS照片库和对应程序沙盒中的主要内容,如果未能解决你的问题,请参考以下文章