CCGraphics.m 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. //
  2. // CCGraphics.m
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 04/02/16.
  6. // Copyright (c) 2016 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. #import "CCGraphics.h"
  24. #import "CCUtility.h"
  25. #import "NCBridgeSwift.h"
  26. @implementation CCGraphics
  27. + (UIImage *)thumbnailImageForVideo:(NSURL *)videoURL atTime:(NSTimeInterval)time
  28. {
  29. AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoURL options:nil];
  30. NSParameterAssert(asset);
  31. AVAssetImageGenerator *assetIG =
  32. [[AVAssetImageGenerator alloc] initWithAsset:asset];
  33. assetIG.appliesPreferredTrackTransform = YES;
  34. assetIG.apertureMode = AVAssetImageGeneratorApertureModeEncodedPixels;
  35. CGImageRef thumbnailImageRef = NULL;
  36. CFTimeInterval thumbnailImageTime = time;
  37. NSError *igError = nil;
  38. thumbnailImageRef =
  39. [assetIG copyCGImageAtTime:CMTimeMake(thumbnailImageTime, 60) actualTime:NULL error:&igError];
  40. if (!thumbnailImageRef) NSLog(@"[LOG] thumbnailImageGenerationError %@", igError );
  41. UIImage *thumbnailImage = thumbnailImageRef ? [[UIImage alloc] initWithCGImage:thumbnailImageRef] : nil;
  42. return thumbnailImage;
  43. }
  44. + (UIImage *)generateImageFromVideo:(NSString *)videoPath
  45. {
  46. NSURL *url = [NSURL fileURLWithPath:videoPath];
  47. NSError *error = NULL;
  48. AVURLAsset* asset = [AVURLAsset URLAssetWithURL:url options:nil];
  49. AVAssetImageGenerator* imageGenerator = [AVAssetImageGenerator assetImageGeneratorWithAsset:asset];
  50. imageGenerator.appliesPreferredTrackTransform = YES;
  51. // CMTime time = CMTimeMake(1, 65);
  52. CGImageRef cgImage = [imageGenerator copyCGImageAtTime:CMTimeMake(0, 1) actualTime:nil error:&error];
  53. if(error) return nil;
  54. UIImage* image = [UIImage imageWithCGImage:cgImage];
  55. CGImageRelease(cgImage);
  56. return image;
  57. }
  58. + (UIImage *)scaleImage:(UIImage *)image toSize:(CGSize)targetSize isAspectRation:(BOOL)aspect
  59. {
  60. CGFloat originRatio = image.size.width / image.size.height;
  61. CGFloat newRatio = targetSize.width / targetSize.height;
  62. CGSize sz;
  63. CGFloat scale = 1.0;
  64. if (!aspect) {
  65. sz = targetSize;
  66. }else {
  67. if (originRatio < newRatio) {
  68. sz.height = targetSize.height;
  69. sz.width = targetSize.height * originRatio;
  70. }else {
  71. sz.width = targetSize.width;
  72. sz.height = targetSize.width / originRatio;
  73. }
  74. }
  75. sz.width /= scale;
  76. sz.height /= scale;
  77. UIGraphicsBeginImageContextWithOptions(sz, NO, UIScreen.mainScreen.scale);
  78. [image drawInRect:CGRectMake(0, 0, sz.width, sz.height)];
  79. UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
  80. UIGraphicsEndImageContext();
  81. return newImage;
  82. }
  83. + (void)createNewImageFrom:(NSString *)fileName ocId:(NSString *)ocId etag:(NSString *)etag typeFile:(NSString *)typeFile
  84. {
  85. UIImage *originalImage;
  86. UIImage *scaleImagePreview;
  87. UIImage *scaleImageIcon;
  88. NSString *fileNamePath = [CCUtility getDirectoryProviderStorageOcId:ocId fileNameView:fileName];
  89. NSString *fileNamePathPreview = [CCUtility getDirectoryProviderStoragePreviewOcId:ocId etag:etag];
  90. NSString *fileNamePathIcon = [CCUtility getDirectoryProviderStorageIconOcId:ocId etag:etag];
  91. if (![CCUtility fileProviderStorageExists:ocId fileNameView:fileName]) return;
  92. // only viedo / image
  93. if (![typeFile isEqualToString: NCBrandGlobal.shared.metadataTypeFileImage] && ![typeFile isEqualToString: NCBrandGlobal.shared.metadataTypeFileVideo]) return;
  94. if ([typeFile isEqualToString: NCBrandGlobal.shared.metadataTypeFileImage]) {
  95. originalImage = [UIImage imageWithContentsOfFile:fileNamePath];
  96. if (originalImage == nil) { return; }
  97. }
  98. if ([typeFile isEqualToString: NCBrandGlobal.shared.metadataTypeFileVideo]) {
  99. // create symbolik link for read video file in temp
  100. [[NSFileManager defaultManager] removeItemAtPath:[NSTemporaryDirectory() stringByAppendingString:@"tempvideo.mp4"] error:nil];
  101. [[NSFileManager defaultManager] linkItemAtPath:fileNamePath toPath:[NSTemporaryDirectory() stringByAppendingString:@"tempvideo.mp4"] error:nil];
  102. originalImage = [self generateImageFromVideo:[NSTemporaryDirectory() stringByAppendingString:@"tempvideo.mp4"]];
  103. }
  104. scaleImagePreview = [self scaleImage:originalImage toSize:CGSizeMake(NCBrandGlobal.shared.sizePreview, NCBrandGlobal.shared.sizePreview) isAspectRation:YES];
  105. scaleImageIcon = [self scaleImage:originalImage toSize:CGSizeMake(NCBrandGlobal.shared.sizeIcon, NCBrandGlobal.shared.sizeIcon) isAspectRation:YES];
  106. scaleImagePreview = [UIImage imageWithData:UIImageJPEGRepresentation(scaleImagePreview, 0.5f)];
  107. scaleImageIcon = [UIImage imageWithData:UIImageJPEGRepresentation(scaleImageIcon, 0.5f)];
  108. // it is request write photo ?
  109. if (scaleImagePreview && scaleImageIcon) {
  110. [UIImageJPEGRepresentation(scaleImagePreview, 0.5) writeToFile:fileNamePathPreview atomically:true];
  111. [UIImageJPEGRepresentation(scaleImageIcon, 0.5) writeToFile:fileNamePathIcon atomically:true];
  112. }
  113. return;
  114. }
  115. + (UIColor *)colorFromHexString:(NSString *)hexString
  116. {
  117. unsigned rgbValue = 0;
  118. NSScanner *scanner = [NSScanner scannerWithString:hexString];
  119. [scanner setScanLocation:1]; // bypass '#' character
  120. [scanner scanHexInt:&rgbValue];
  121. return [UIColor colorWithRed:((rgbValue & 0xFF0000) >> 16)/255.0 green:((rgbValue & 0xFF00) >> 8)/255.0 blue:(rgbValue & 0xFF)/255.0 alpha:1.0];
  122. }
  123. + (UIImage *)changeThemingColorImage:(UIImage *)image width:(CGFloat)width height:(CGFloat)height color:(UIColor *)color
  124. {
  125. CGRect rect = CGRectMake(0, 0, width / (2 / UIScreen.mainScreen.scale), height / (2 / UIScreen.mainScreen.scale));
  126. UIGraphicsBeginImageContext(rect.size);
  127. CGContextRef context = UIGraphicsGetCurrentContext();
  128. CGContextClipToMask(context, rect, image.CGImage);
  129. if (color)
  130. CGContextSetFillColorWithColor(context, [color CGColor]);
  131. CGContextFillRect(context, rect);
  132. UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
  133. UIGraphicsEndImageContext();
  134. return [UIImage imageWithCGImage:img.CGImage scale:UIScreen.mainScreen.scale orientation: UIImageOrientationDownMirrored];
  135. }
  136. + (void)settingThemingColor:(NSString *)themingColor themingColorElement:(NSString *)themingColorElement themingColorText:(NSString *)themingColorText
  137. {
  138. UIColor *newColor, *newColorElement, *newColorText;
  139. // COLOR
  140. if (themingColor.length == 7) {
  141. newColor = [CCGraphics colorFromHexString:themingColor];
  142. } else {
  143. newColor = NCBrandColor.shared.customer;
  144. }
  145. // COLOR TEXT
  146. if (themingColorText.length == 7) {
  147. newColorText = [CCGraphics colorFromHexString:themingColorText];
  148. } else {
  149. newColorText = NCBrandColor.shared.customerText;
  150. }
  151. // COLOR ELEMENT
  152. if (themingColorElement.length == 7) {
  153. newColorElement = [CCGraphics colorFromHexString:themingColorElement];
  154. } else {
  155. if ([themingColorText isEqualToString:@"#000000"])
  156. newColorElement = [UIColor blackColor];
  157. else
  158. newColorElement = newColor;
  159. }
  160. NCBrandColor.shared.brand = newColor;
  161. NCBrandColor.shared.brandElement = newColorElement;
  162. NCBrandColor.shared.brandText = newColorText;
  163. }
  164. @end