ImageData.m 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2012-2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. #import "ImageData.h"
  21. #import <ImageIO/ImageIO.h>
  22. @implementation ImageData
  23. @dynamic height;
  24. @dynamic width;
  25. @dynamic data;
  26. - (UIImage*)uiImage {
  27. return [UIImage imageWithData:self.data];
  28. }
  29. - (NSString *)getCaption {
  30. if (self.data)
  31. return [ImageData getCaptionForImageData:self.data];
  32. return nil;
  33. }
  34. - (void)setCaption:(NSString *)caption {
  35. self.data = [ImageData addCaption:caption toImageData:self.data];
  36. }
  37. - (NSDictionary *)getMetadata {
  38. CGImageSourceRef source = CGImageSourceCreateWithData((CFDataRef)self.data, NULL);
  39. if (source == NULL)
  40. return nil;
  41. NSDictionary *metadata = (NSDictionary *) CFBridgingRelease(CGImageSourceCopyPropertiesAtIndex(source,0,NULL));
  42. CFRelease(source);
  43. return metadata;
  44. }
  45. + (NSString *)getCaptionForImageData:(NSData *)imageData {
  46. CGImageSourceRef source = CGImageSourceCreateWithData((CFDataRef)imageData, NULL);
  47. if (source == NULL)
  48. return nil;
  49. NSDictionary *metadata = (NSDictionary *) CFBridgingRelease(CGImageSourceCopyPropertiesAtIndex(source,0,NULL));
  50. CFRelease(source);
  51. NSMutableDictionary *tiffData = [metadata objectForKey:(NSString *)kCGImagePropertyTIFFDictionary];
  52. NSString *author = [tiffData objectForKey:(NSString *)kCGImagePropertyTIFFArtist];
  53. return author;
  54. }
  55. + (NSData *)addCaption:(NSString *)caption toImageData:(NSData *)imageData {
  56. CGImageSourceRef source = CGImageSourceCreateWithData((CFDataRef)imageData, NULL);
  57. if (source == NULL)
  58. return imageData;
  59. NSDictionary *metadata = (NSDictionary *) CFBridgingRelease(CGImageSourceCopyPropertiesAtIndex(source,0,NULL));
  60. CFStringRef uti = CGImageSourceGetType(source);
  61. NSMutableDictionary *metadataMutable = [metadata mutableCopy];
  62. NSMutableDictionary *tiffDataMutable = [metadataMutable objectForKey:(NSString *)kCGImagePropertyTIFFDictionary];
  63. if (caption)
  64. [tiffDataMutable setObject:caption forKey:(NSString *)kCGImagePropertyTIFFArtist];
  65. else
  66. [tiffDataMutable removeObjectForKey:(NSString *)kCGImagePropertyTIFFArtist];
  67. NSMutableData *newData = [NSMutableData data];
  68. CGImageDestinationRef newImageDestination = CGImageDestinationCreateWithData((__bridge CFMutableDataRef)newData, uti, 1, NULL);
  69. if (!newImageDestination) {
  70. CFRelease(source);
  71. return imageData;
  72. }
  73. CGImageDestinationAddImageFromSource(newImageDestination, source, 0, (__bridge CFDictionaryRef) metadataMutable);
  74. BOOL success = CGImageDestinationFinalize(newImageDestination);
  75. CFRelease(newImageDestination);
  76. CFRelease(source);
  77. if (success) {
  78. return newData;
  79. }
  80. return imageData;
  81. }
  82. @end