美文网首页iOS 开发 学习
iOS 将图片保存到App中,并取出显示

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

作者: 清蘂翅膀的技术 | 来源:发表于2017-05-16 14:12 被阅读11次

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中,并取出显示

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