iOS 将图片保存到App中,并取出显示

1、保存至App文件中

首先在项目中创建图片文件夹(相册)

NSFileManager*fileManager = [NSFileManagerdefaultManager];

NSString*filePath = [JGManagerToolimagePath];

[fileManagercreateDirectoryAtPath:filePathwithIntermediateDirectories:YESattributes:nilerror:nil];

- (UIImage*)imageAddImage {

UIImage*logoimage = [UIImageimageNamed:@"水印"];

UIGraphicsBeginImageContext(CGSizeMake(self.image.size.width,self.image.size.height));

CGRectimageRect =CGRectMake(0,0,self.image.size.width,self.image.size.height);

[logoimagedrawInRect:imageRect];

UIImage*newImage =UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

UIImage*targetImage = [[UIImagealloc]init];

targetImage = [targetImageimageWithLogoImage:self.imagelogo:newImage];

returntargetImage;

}

- (NSString*)imagePath {

NSString*documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)firstObject];

NSString*filePath = [documentsPathstringByAppendingPathComponent:@"imageFile"];

returnfilePath;

}

- (void)saveLocalPhoto {

UIImage*image = [selfimageAddImage];

NSData*data;

if(UIImagePNGRepresentation(image) ==nil) {

data =UIImageJPEGRepresentation(image,1);

}else{

data =UIImagePNGRepresentation(image);

}

NSFileManager*fileManager = [NSFileManagerdefaultManager];

NSString*filePath = [selfimagePath];

NSDate*date = [NSDatedate];

NSDateFormatter*dateFormatter = [[NSDateFormatteralloc]init];

dateFormatter.dateFormat=@"yyyyMMddhhmmss";

NSString*str = [dateFormatterstringFromDate:date];

if([fileManagercreateFileAtPath:[filePathstringByAppendingString:[NSStringstringWithFormat:@"/%@.png",str]]contents:dataattributes:nil]) {

[JGManagerToolsetSVProgressType:SVProgressHUDSuccessSVProgressText:@"保存成功"SVProgressProgress:0SVProgressImage:nil];

[JGManagerTooldissSVProgress:2.0];

[selfdismissViewControllerAnimated:YEScompletion:nil];

}else{

[JGManagerToolsetSVProgressType:SVProgressHUDErrorSVProgressText:@"保存失败"SVProgressProgress:0SVProgressImage:nil];

[JGManagerTooldissSVProgress:1.0];

}

}

2、取出图片显示

只要取出图片路劲就行

NSString*imagePath = 图片路径;

NSData*data = [NSDatadataWithContentsOfFile:imagePath];

[UIImageimageWithData:data]

你可能感兴趣的:(iOS 将图片保存到App中,并取出显示)