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

1、保存至App文件中
首先在项目中创建图片文件夹(相册)

NSFileManager *fileManager = [NSFileManager defaultManager];

NSString *filePath = [JGManagerTool imagePath];

 [fileManager createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];

 

- (UIImage *)imageAddImage {

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

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

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

    [logoimage drawInRect:imageRect];

    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    UIImage *targetImage = [[UIImage alloc]init];

    targetImage = [targetImage imageWithLogoImage:self.image logo:newImage];

    return targetImage;

}

- (NSString *)imagePath {


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

    NSString *filePath = [documentsPath stringByAppendingPathComponent:@"imageFile"];

    return filePath;

 

}

- (void)saveLocalPhoto {

    UIImage *image = [self imageAddImage];

    NSData *data;

    if (UIImagePNGRepresentation(image) == nil) {

        

        data = UIImageJPEGRepresentation(image, 1);

        

    } else {

        

        data = UIImagePNGRepresentation(image);

        

    }

    

    NSFileManager *fileManager = [NSFileManager defaultManager];

    NSString *filePath = [self imagePath];

   

    NSDate *date = [NSDate date];

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];

    dateFormatter.dateFormat = @"yyyyMMddhhmmss";

    NSString *str = [dateFormatter stringFromDate:date];

    

    if ([fileManager createFileAtPath:[filePath stringByAppendingString:[NSString stringWithFormat:@"/%@.png",str]] contents:data attributes:nil]) {

        [JGManagerTool setSVProgressType:SVProgressHUDSuccess SVProgressText:@"保存成功" SVProgressProgress:0 SVProgressImage:nil];

        [JGManagerTool dissSVProgress:2.0];

        [self dismissViewControllerAnimated:YES completion:nil];

    } else {

        [JGManagerTool setSVProgressType:SVProgressHUDError SVProgressText:@"保存失败" SVProgressProgress:0 SVProgressImage:nil];

        [JGManagerTool dissSVProgress:1.0];

    }


}

2、取出图片显示

只要取出图片路劲就行

NSString *imagePath = 图片路径;

NSData *data = [NSData dataWithContentsOfFile:imagePath];

 

[UIImage imageWithData:data]

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值