CCGraphics.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. //
  2. // CCGraphics.m
  3. // Crypto Cloud Technology Nextcloud
  4. //
  5. // Created by Marino Faggiana on 04/02/16.
  6. // Copyright (c) 2014 TWS. All rights reserved.
  7. //
  8. // Author Marino Faggiana <m.faggiana@twsweb.it>
  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 "AppDelegate.h"
  25. #import "CCUtility.h"
  26. #import "NSString+TruncateToWidth.h"
  27. @implementation CCGraphics
  28. + (UIImage *)thumbnailImageForVideo:(NSURL *)videoURL atTime:(NSTimeInterval)time
  29. {
  30. AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoURL options:nil];
  31. NSParameterAssert(asset);
  32. AVAssetImageGenerator *assetIG =
  33. [[AVAssetImageGenerator alloc] initWithAsset:asset];
  34. assetIG.appliesPreferredTrackTransform = YES;
  35. assetIG.apertureMode = AVAssetImageGeneratorApertureModeEncodedPixels;
  36. CGImageRef thumbnailImageRef = NULL;
  37. CFTimeInterval thumbnailImageTime = time;
  38. NSError *igError = nil;
  39. thumbnailImageRef =
  40. [assetIG copyCGImageAtTime:CMTimeMake(thumbnailImageTime, 60) actualTime:NULL error:&igError];
  41. if (!thumbnailImageRef) NSLog(@"[LOG] thumbnailImageGenerationError %@", igError );
  42. UIImage *thumbnailImage = thumbnailImageRef ? [[UIImage alloc] initWithCGImage:thumbnailImageRef] : nil;
  43. return thumbnailImage;
  44. }
  45. // mix two image
  46. + (UIImage *)overlayImage:(UIImage *)backgroundImage watermarkImage:(UIImage *)watermarkImage where:(NSString *)where
  47. {
  48. // example watermarkImage = [UIImage imageNamed:@"lock"];
  49. UIGraphicsBeginImageContext(backgroundImage.size);
  50. [backgroundImage drawInRect:CGRectMake(0, 0, backgroundImage.size.width, backgroundImage.size.height)];
  51. if ([where isEqualToString:@"right"]) [watermarkImage drawInRect:CGRectMake(backgroundImage.size.width - watermarkImage.size.width, backgroundImage.size.height - watermarkImage.size.height, watermarkImage.size.width, watermarkImage.size.height)];
  52. if ([where isEqualToString:@"left"]) [watermarkImage drawInRect:CGRectMake(0, backgroundImage.size.height - watermarkImage.size.height, backgroundImage.size.width - watermarkImage.size.width, backgroundImage.size.height - watermarkImage.size.height)];
  53. UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
  54. UIGraphicsEndImageContext();
  55. return result;
  56. }
  57. + (UIImage *)generateImageFromVideo:(NSString *)videoPath
  58. {
  59. NSURL *url = [NSURL fileURLWithPath:videoPath];
  60. NSError *error = NULL;
  61. AVURLAsset* asset = [AVURLAsset URLAssetWithURL:url options:nil];
  62. AVAssetImageGenerator* imageGenerator = [AVAssetImageGenerator assetImageGeneratorWithAsset:asset];
  63. imageGenerator.appliesPreferredTrackTransform = YES;
  64. // CMTime time = CMTimeMake(1, 65);
  65. CGImageRef cgImage = [imageGenerator copyCGImageAtTime:CMTimeMake(0, 1) actualTime:nil error:&error];
  66. if(error) return nil;
  67. UIImage* image = [UIImage imageWithCGImage:cgImage];
  68. CGImageRelease(cgImage);
  69. return image;
  70. }
  71. + (UIImage *)scaleImage:(UIImage *)image toSize:(CGSize)targetSize
  72. {
  73. //If scaleFactor is not touched, no scaling will occur
  74. CGFloat scaleFactor = 1.0;
  75. //Deciding which factor to use to scale the image (factor = targetSize / imageSize)
  76. if (image.size.width > targetSize.width || image.size.height > targetSize.height)
  77. if (!((scaleFactor = (targetSize.width / image.size.width)) > (targetSize.height / image.size.height))) //scale to fit width, or
  78. scaleFactor = targetSize.height / image.size.height; // scale to fit heigth.
  79. UIGraphicsBeginImageContext(targetSize);
  80. //Creating the rect where the scaled image is drawn in
  81. CGRect rect = CGRectMake((targetSize.width - image.size.width * scaleFactor) / 2,
  82. (targetSize.height - image.size.height * scaleFactor) / 2,
  83. image.size.width * scaleFactor, image.size.height * scaleFactor);
  84. //Draw the image into the rect
  85. [image drawInRect:rect];
  86. //Saving the image, ending image context
  87. UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
  88. UIGraphicsEndImageContext();
  89. return scaledImage;
  90. }
  91. + (UIImage *)scaleImage:(UIImage *)image toSize:(CGSize)targetSize isAspectRation:(BOOL)aspect
  92. {
  93. CGFloat originRatio = image.size.width / image.size.height;
  94. CGFloat newRatio = targetSize.width / targetSize.height;
  95. CGSize sz;
  96. CGFloat scale = 1.0;
  97. if (!aspect) {
  98. sz = targetSize;
  99. }else {
  100. if (originRatio < newRatio) {
  101. sz.height = targetSize.height;
  102. sz.width = targetSize.height * originRatio;
  103. }else {
  104. sz.width = targetSize.width;
  105. sz.height = targetSize.width / originRatio;
  106. }
  107. }
  108. sz.width /= scale;
  109. sz.height /= scale;
  110. UIGraphicsBeginImageContextWithOptions(sz, NO, scale);
  111. [image drawInRect:CGRectMake(0, 0, sz.width, sz.height)];
  112. UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
  113. UIGraphicsEndImageContext();
  114. return newImage;
  115. }
  116. + (UIImage *)createNewImageFrom:(NSString *)fileName directoryUser:(NSString *)directoryUser fileNameTo:(NSString *)fileNameTo fileNamePrint:(NSString *)fileNamePrint size:(NSString *)size imageForUpload:(BOOL)imageForUpload typeFile:(NSString *)typeFile writePreview:(BOOL)writePreview optimizedFileName:(BOOL)optimizedFileName
  117. {
  118. UIImage *originalImage;
  119. UIImage *scaleImage;
  120. CGRect rect;
  121. CGFloat width, height;
  122. NSString *ext = [[fileNamePrint pathExtension] lowercaseString];
  123. if ([[directoryUser substringFromIndex: [directoryUser length] - 1] isEqualToString:@"/"]) directoryUser = [directoryUser substringToIndex:[directoryUser length]-1];
  124. if (![[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@", directoryUser, fileName]]) return nil;
  125. // only viedo / image
  126. if (![typeFile isEqualToString: k_metadataTypeFile_image] && ![typeFile isEqualToString: k_metadataTypeFile_video]) return nil;
  127. if ([typeFile isEqualToString: k_metadataTypeFile_image]) {
  128. originalImage = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@", directoryUser, fileName]];
  129. }
  130. if ([typeFile isEqualToString: k_metadataTypeFile_video]) {
  131. // create symbolik link for read video file in temp
  132. [[NSFileManager defaultManager] removeItemAtPath:[NSTemporaryDirectory() stringByAppendingString:@"tempvideo.mp4"] error:nil];
  133. [[NSFileManager defaultManager] linkItemAtPath:[NSString stringWithFormat:@"%@/%@", directoryUser, fileName] toPath:[NSTemporaryDirectory() stringByAppendingString:@"tempvideo.mp4"] error:nil];
  134. originalImage = [self generateImageFromVideo:[NSTemporaryDirectory() stringByAppendingString:@"tempvideo.mp4"]];
  135. }
  136. if ([size isEqualToString:@"xs"]) scaleImage = [self scaleImage:originalImage toSize:CGSizeMake(32, 32)];
  137. if ([size isEqualToString:@"s"]) scaleImage = [self scaleImage:originalImage toSize:CGSizeMake(64, 64)];
  138. if ([size isEqualToString:@"m"]) scaleImage = [self scaleImage:originalImage toSize:CGSizeMake(128, 128)];
  139. if ([size isEqualToString:@"l"]) scaleImage = [self scaleImage:originalImage toSize:CGSizeMake(640, 640)];
  140. if ([size isEqualToString:@"xl"]) scaleImage = [self scaleImage:originalImage toSize:CGSizeMake(1024, 1024)];
  141. scaleImage = [UIImage imageWithData:UIImageJPEGRepresentation(scaleImage, 0.5f)];
  142. // it is request write photo preview ?
  143. if (writePreview && scaleImage) {
  144. if (imageForUpload) {
  145. // write image preview in tmp for plist
  146. [self saveIcoWithFileID:fileNameTo image:scaleImage writeToFile:[NSTemporaryDirectory() stringByAppendingString:fileNameTo] copy:NO move:NO fromPath:nil toPath:nil];
  147. // if it is preview for Upload then trasform it in gray scale
  148. //TODO: Crash with swift
  149. scaleImage = [scaleImage grayscale];
  150. [self saveIcoWithFileID:fileNameTo image:scaleImage writeToFile:[NSString stringWithFormat:@"%@/%@.ico", directoryUser, fileNameTo] copy:NO move:NO fromPath:nil toPath:nil];
  151. } else {
  152. [self saveIcoWithFileID:fileNameTo image:scaleImage writeToFile:[NSString stringWithFormat:@"%@/%@.ico", directoryUser, fileNameTo] copy:NO move:NO fromPath:nil toPath:nil];
  153. }
  154. }
  155. // Optimized photos resolution
  156. // Resize image 640 x 480 ( with proportion : 1,333)
  157. if (originalImage.size.height < originalImage.size.width) {
  158. // (lanscape)
  159. width = 640;
  160. height = 480;
  161. } else {
  162. // (portrait)
  163. height = 640;
  164. width = 480;
  165. }
  166. // Optimized photos resolution
  167. if ([typeFile isEqualToString: k_metadataTypeFile_image] && [ext isEqualToString:@"gif"] == NO && optimizedFileName && scaleImage && (originalImage.size.width > width || originalImage.size.height > height)) {
  168. // conversion scale proportion
  169. if (height > width) {
  170. float proportion = originalImage.size.height / originalImage.size.width;
  171. width = height / proportion;
  172. } else {
  173. float proportion = originalImage.size.width / originalImage.size.height;
  174. height = width / proportion;
  175. }
  176. rect = CGRectMake(0,0,width,height);
  177. UIGraphicsBeginImageContext(rect.size);
  178. [originalImage drawInRect:rect];
  179. UIImage *resizeImage = UIGraphicsGetImageFromCurrentImageContext();
  180. UIGraphicsEndImageContext();
  181. resizeImage = [UIImage imageWithData:UIImageJPEGRepresentation(resizeImage, 0.5f)];
  182. if (resizeImage) [UIImagePNGRepresentation(resizeImage) writeToFile:[NSString stringWithFormat:@"%@/%@", directoryUser, fileNameTo] atomically: YES];
  183. }
  184. return scaleImage;
  185. }
  186. + (void)saveIcoWithFileID:(NSString *)fileID image:(UIImage *)image writeToFile:(NSString *)writeToFile copy:(BOOL)copy move:(BOOL)move fromPath:(NSString *)fromPath toPath:(NSString *)toPath
  187. {
  188. if (writeToFile)
  189. [UIImagePNGRepresentation(image) writeToFile:writeToFile atomically: YES];
  190. if (copy)
  191. [CCUtility copyFileAtPath:fromPath toPath:toPath];
  192. if (move)
  193. [[NSFileManager defaultManager] moveItemAtPath:fromPath toPath:toPath error:nil];
  194. #ifndef EXTENSION
  195. if (image && fileID)
  196. [app.icoImagesCache setObject:image forKey:fileID];
  197. #endif
  198. }
  199. + (UIColor *)colorFromHexString:(NSString *)hexString
  200. {
  201. unsigned rgbValue = 0;
  202. NSScanner *scanner = [NSScanner scannerWithString:hexString];
  203. [scanner setScanLocation:1]; // bypass '#' character
  204. [scanner scanHexInt:&rgbValue];
  205. return [UIColor colorWithRed:((rgbValue & 0xFF0000) >> 16)/255.0 green:((rgbValue & 0xFF00) >> 8)/255.0 blue:(rgbValue & 0xFF)/255.0 alpha:1.0];
  206. }
  207. + (UIImage *)changeThemingColorImage:(UIImage *)image color:(UIColor *)color
  208. {
  209. if (k_option_use_themingColor == NO || [color isEqual:k_color_brand_standard])
  210. return image;
  211. CGRect rect = CGRectMake(0, 0, image.size.width*2, image.size.height*2);
  212. UIGraphicsBeginImageContext(rect.size);
  213. CGContextRef context = UIGraphicsGetCurrentContext();
  214. CGContextClipToMask(context, rect, image.CGImage);
  215. CGContextSetFillColorWithColor(context, [color CGColor]);
  216. CGContextFillRect(context, rect);
  217. UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
  218. UIGraphicsEndImageContext();
  219. return [UIImage imageWithCGImage:img.CGImage scale:2.0 orientation: UIImageOrientationDownMirrored];
  220. }
  221. @end
  222. // ------------------------------------------------------------------------------------------------------
  223. // MARK: Avatar
  224. // ------------------------------------------------------------------------------------------------------
  225. @implementation CCAvatar
  226. - (id)initWithImage:(UIImage *)image borderColor:(UIColor*)borderColor borderWidth:(float)borderWidth
  227. {
  228. self = [super initWithImage:image];
  229. float cornerRadius = self.frame.size.height/2.0f;
  230. CALayer *layer = [self layer];
  231. [layer setMasksToBounds:YES];
  232. [layer setCornerRadius: cornerRadius];
  233. [layer setBorderWidth: borderWidth];
  234. [layer setBorderColor:[borderColor CGColor]];
  235. return self;
  236. }
  237. @end