IOS收藏

保存图片至iOS图库和沙盒目录

2015-09-15  本文已影响906人  ldjhust

1.保存到iOS照片库需要引入QuartzCore.framework框架,具体代码如下:

.h文件

#import

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: filePathatomically:YES]; // 保存成功会返回YES

上一篇下一篇

猜你喜欢

热点阅读