MediaBrowserPhoto.m 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2014-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 "MWPhoto.h"
  21. //
  22. // MediaBrowserPhoto.m
  23. // Threema
  24. //
  25. // Copyright (c) 2014 Threema GmbH. All rights reserved.
  26. //
  27. #import "MediaBrowserPhoto.h"
  28. #import "ImageMessageLoader.h"
  29. #import "NSString+Hex.m"
  30. @interface MediaBrowserPhoto ()
  31. @property NSURL *photoURL;
  32. @property BOOL thumbnail;
  33. @end
  34. @implementation MediaBrowserPhoto
  35. @synthesize underlyingImage = _underlyingImage; // synth property from protocol
  36. + (instancetype)photoWithImageMessage:(ImageMessage *)imageMessage thumbnail:(BOOL)thumbnail {
  37. return [[MediaBrowserPhoto alloc] initWithImageMessage:imageMessage thumbnail:thumbnail];
  38. }
  39. - (id)initWithImageMessage:(ImageMessage *)imageMessage thumbnail:(BOOL)thumbnail {
  40. if ((self = [super init])) {
  41. _imageMessage = imageMessage;
  42. _thumbnail = thumbnail;
  43. _isUtiPreview = false;
  44. }
  45. return self;
  46. }
  47. -(id)sourceReference {
  48. return _imageMessage;
  49. }
  50. #pragma mark - MWPhoto Protocol Methods
  51. - (UIImage *)underlyingImage {
  52. return _underlyingImage;
  53. }
  54. - (void)loadUnderlyingImageAndNotify {
  55. [self performLoadUnderlyingImageAndNotify];
  56. }
  57. // Set the underlyingImage
  58. - (void)performLoadUnderlyingImageAndNotify {
  59. if (_thumbnail) {
  60. _underlyingImage = _imageMessage.thumbnail.uiImage;
  61. }
  62. else {
  63. _underlyingImage = _imageMessage.image != nil ? _imageMessage.image.uiImage : _imageMessage.thumbnail.uiImage;
  64. }
  65. dispatch_async(dispatch_get_main_queue(), ^{
  66. [[NSNotificationCenter defaultCenter] postNotificationName:MWPHOTO_LOADING_DID_END_NOTIFICATION object:self];
  67. });
  68. }
  69. - (void)unloadUnderlyingImage {
  70. _underlyingImage = nil;
  71. }
  72. - (BOOL)showControls {
  73. return NO;
  74. }
  75. -(BOOL)canScaleImage {
  76. return YES;
  77. }
  78. -(NSURL *)urlForExportData:(NSString *)tmpFileName {
  79. NSString *fileName = [self.imageMessage getFilename];
  80. if (fileName == nil) {
  81. fileName = tmpFileName;
  82. }
  83. _photoURL = [[[NSURL fileURLWithPath:NSTemporaryDirectory() isDirectory:YES] URLByAppendingPathComponent:fileName] URLByAppendingPathExtension:MEDIA_EXTENSION_IMAGE];
  84. NSData *imageData = _imageMessage.image.data;
  85. if (![imageData writeToURL:_photoURL atomically:NO]) {
  86. return nil;
  87. }
  88. return _photoURL;
  89. }
  90. - (void)cancelAnyLoading {
  91. }
  92. - (NSString *)accessibilityLabelForContent {
  93. NSString *date = [DateFormatter accessibilityDateTime:_imageMessage.remoteSentDate];
  94. return [NSString stringWithFormat:@"%@. %@", NSLocalizedString(@"image", nil), date];
  95. }
  96. @end