在App中存取照片

1、存照片

 1 - (void)setPhoto:(UIImage *)image{

 2     UIImage *image = image;

 3      4

 5     //0.5代表的是压缩率,1为不压缩,图像质量最高,0为最高压缩,图像质量最低

 6     NSData *imageData = UIImageJPEGRepresentation(image, 0.5);

 7     NSString *relativePath = @"UserPhoto.jpg";

 8     NSString *path = [self documentsPathForFileName:relativePath];

 9     NSLog(@"path %@", path);

10     [imageData writeToFile:path atomically:YES];

11     [[NSUserDefaults standardUserDefaults] setObject:path forKey:kUserImagePath];

12     [[NSUserDefaults standardUserDefaults] synchronize];

13 }

14 

15 - (NSString*)documentsPathForFileName:(NSString*)name{

16     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

17     NSString *documentsPath = [paths objectAtIndex:0];

18     return [documentsPath stringByAppendingPathComponent:name];

19 }

2、读取照片

NSString *imagePath = [[NSUserDefaults standardUserDefaults] objectForKey:kUserImagePath];

    if (imagePath) {

       image = [UIImage imageWithData:[NSData dataWithContentsOfFile:imagePath]];

    }

 

这里的主要思路是,将照片存入应用程序的文件下,并将路径存入NSUserDefaults

读照片时,从NSUserDefaults读出路径,之后读取照片。

 

该方法的来源:http://stackoverflow.com/questions/6648518/save-images-in-nsuserdefaults

你可能感兴趣的:(APP)