iOS 将图片保存到App中,并取…

来源:互联网 发布:赵云人气 知乎 编辑:程序博客网 时间:2024/06/05 08:13
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);

   [logoimage drawInRect:imageRect];

   UIImage*newImage= UIGraphicsGetImageFromCurrentImageContext();

   UIGraphicsEndImageContext();

   UIImage*targetImage =[[UIImagealloc]init];

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

   returntargetImage;

}

-(NSString*)imagePath{


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

   NSString*filePath= [documentsPath stringByAppendingPathComponent:@"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= [self imagePath];

  

   NSDate*date =[NSDatedate];

   NSDateFormatter*dateFormatter= [[NSDateFormatteralloc]init];

   dateFormatter.dateFormat=@"yyyyMMddhhmmss";

   NSString*str =[dateFormatter stringFromDate: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]

阅读全文
0 0
原创粉丝点击