IOS imageIO.framework框架修改照片图片的拍摄时间等exif信息

   今天学习了imageIO底层框架,由于公司有一个需求用imagepicker拍出来的照片没有exif信息,需要把照片添加设置拍摄时间等exif信息,所以上网查到一些资料,在这里整理一下希望对大家有所帮助

   一,在使用imageIO框架时需要导入头文件

#import <ImageIO/ImageIO.h>

这样可以使用ImageIO里面的

CGImageSourceRef CGImageDestinationRef两个类了,CGImageSourceRef功能是读取图像数据,读取图像的缩略图以及图像的属性exif信息等.CGImageDestinationRef可以将数据写到图片中如exif信息

二,CGImageSourceRef的使用

- (void)getdata:(NSString*)imagePath//传入图片的路径

{

    CGImageSourceRef imageSource = CGImageSourceCreateWithURL((__bridge CFURLRef)[NSURL fileURLWithPath:imagePath],NULL);

    NSDictionary     * imageProperty=(NSDictionary*)CGImageSourceCopyPropertiesAtIndex(imageSource, 0, NULL);

    NSDictionary* exifDictionary=[imageProperty valueForKey:(NSString*)kCGImagePropertyExifDictionary];//获取图片的exif信息

    NSDictionary*tiffDictonary=[imageProperty valueForKey:(NSString*)kCGImagePropertyTIFFDictionary];//获取图片的tiff信息

    CFStringRef fileUTI=CGImageSourceGetType(imageSource);

    CFStringRef fileTypeDes = CFCopyDescription(fileUTI);

    NSString*   filetype=(NSString*)fileTypeDes;//获取图片的属性

    

}

三,CGImageDestinationRef的使用修改exif信息

- (void)saveExif:(NSString*)imagePath//传入图片的路径

{

    

   

    

    CGImageSourceRef imageSource = CGImageSourceCreateWithURL((__bridge CFURLRef)[NSURL fileURLWithPath:imagePath],NULL);


    NSDictionary *dict = (NSDictionary*)CGImageSourceCopyPropertiesAtIndex(imageSource, 0, NULL);

    NSMutableDictionary *dictInfo = [NSMutableDictionary dictionaryWithDictionary:dict];

    CFStringRef imagUTI = CGImageSourceGetType(imageSource);

    

   

    CGImageDestinationRef _imageDestination = CGImageDestinationCreateWithURL((CFURLRef)[NSURL fileURLWithPath:imagePath], imagUTI, 1, NULL);

    // modify dict before add

   

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

        [formatter setDateFormat:@"YYYY:MM:dd hh:mm:ss"];

        NSString *now = [formatter stringFromDate:[NSDate date]];

   //设置图片的拍摄日期     

        [exifDictInfo setObject:now forKey:(NSString*)kCGImagePropertyExifDateTimeOriginal];

        [exifDictInfo setObject:now forKey:(NSString*)kCGImagePropertyExifDateTimeDigitized];

    

        

        // modify orientation

       

        [dictInfo setValue:exifDictInfo forKey:(NSString*)kCGImagePropertyExifDictionary];

    

    

    CGImageDestinationAddImageFromSource(_imageDestination, imageSource, 0, (CFDictionaryRef)dictInfo);

    CGImageDestinationFinalize(_imageDestination);

    }

}

这里只修改了图片的拍摄时间,大家可以试着去修改图片的方向等信息

 [dictInfo setValue:[NSNumber numberWithInteger:newImageOrientation]forKey:(NSString*)kCGImagePropertyOrientation];

 NSInteger currentOrientation = [[dict valueForKey:(NSString*)kCGImagePropertyOrientation] intValue];//获取当前的方向



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值