1//
2//  EXImageUtils.m
3//  Exponent
4//
5//  Created by Stanisław Chmiela on 17.11.2017.
6//  Copyright © 2017 650 Industries. All rights reserved.
7//
8
9#import "EXImageUtils.h"
10
11@implementation EXImageUtils
12
13+ (UIImage *)generatePhotoOfSize:(CGSize)size
14{
15  CGRect rect = CGRectMake(0, 0, size.width, size.height);
16  UIImage *image;
17  UIGraphicsBeginImageContextWithOptions(size, YES, 0);
18  UIColor *color = [UIColor blackColor];
19  [color setFill];
20  UIRectFill(rect);
21  NSDate *currentDate = [NSDate date];
22  NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
23  [dateFormatter setDateFormat:@"dd.MM.YY HH:mm:ss"];
24  NSString *text = [dateFormatter stringFromDate:currentDate];
25  NSDictionary *attributes = [NSDictionary dictionaryWithObjects: @[[UIFont systemFontOfSize:18.0], [UIColor orangeColor]]
26                                                         forKeys: @[NSFontAttributeName, NSForegroundColorAttributeName]];
27  [text drawAtPoint:CGPointMake(size.width * 0.1, size.height * 0.9) withAttributes:attributes];
28  image = UIGraphicsGetImageFromCurrentImageContext();
29  UIGraphicsEndImageContext();
30  return image;
31}
32
33+ (UIImage *)cropImage:(UIImage *)image toRect:(CGRect)rect
34{
35  CGImageRef takenCGImage = image.CGImage;
36  CGImageRef cropCGImage = CGImageCreateWithImageInRect(takenCGImage, rect);
37  image = [UIImage imageWithCGImage:cropCGImage scale:image.scale orientation:image.imageOrientation];
38  CGImageRelease(cropCGImage);
39  return image;
40}
41
42+ (NSString *)writeImage:(NSData *)image toPath:(NSString *)path
43{
44  [image writeToFile:path atomically:YES];
45  NSURL *fileURL = [NSURL fileURLWithPath:path];
46  return [fileURL absoluteString];
47}
48
49+ (void)updatePhotoMetadata:(CMSampleBufferRef)imageSampleBuffer withAdditionalData:(NSDictionary *)additionalData inResponse:(NSMutableDictionary *)response
50{
51  CFDictionaryRef exifAttachments = CMGetAttachment(imageSampleBuffer, kCGImagePropertyExifDictionary, NULL);
52  NSMutableDictionary *metadata = (__bridge NSMutableDictionary *)exifAttachments;
53  metadata[(NSString *)kCGImagePropertyExifPixelYDimension] = response[@"width"];
54  metadata[(NSString *)kCGImagePropertyExifPixelXDimension] = response[@"height"];
55
56  for (id key in additionalData) {
57    metadata[key] = additionalData[key];
58  }
59
60  NSDictionary *gps = metadata[(NSString *)kCGImagePropertyGPSDictionary];
61
62  if (gps) {
63    for (NSString *gpsKey in gps) {
64      metadata[[@"GPS" stringByAppendingString:gpsKey]] = gps[gpsKey];
65    }
66  }
67
68  response[@"exif"] = metadata;
69}
70
71@end
72