CCGraphics.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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 "NSString+TruncateToWidth.h"
  26. #import "NCBridgeSwift.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. + (UIImage *)generateImageFromVideo:(NSString *)videoPath
  46. {
  47. NSURL *url = [NSURL fileURLWithPath:videoPath];
  48. NSError *error = NULL;
  49. AVURLAsset* asset = [AVURLAsset URLAssetWithURL:url options:nil];
  50. AVAssetImageGenerator* imageGenerator = [AVAssetImageGenerator assetImageGeneratorWithAsset:asset];
  51. imageGenerator.appliesPreferredTrackTransform = YES;
  52. // CMTime time = CMTimeMake(1, 65);
  53. CGImageRef cgImage = [imageGenerator copyCGImageAtTime:CMTimeMake(0, 1) actualTime:nil error:&error];
  54. if(error) return nil;
  55. UIImage* image = [UIImage imageWithCGImage:cgImage];
  56. CGImageRelease(cgImage);
  57. return image;
  58. }
  59. + (UIImage *)scaleImage:(UIImage *)image toSize:(CGSize)targetSize isAspectRation:(BOOL)aspect
  60. {
  61. CGFloat originRatio = image.size.width / image.size.height;
  62. CGFloat newRatio = targetSize.width / targetSize.height;
  63. CGSize sz;
  64. CGFloat scale = 1.0;
  65. if (!aspect) {
  66. sz = targetSize;
  67. }else {
  68. if (originRatio < newRatio) {
  69. sz.height = targetSize.height;
  70. sz.width = targetSize.height * originRatio;
  71. }else {
  72. sz.width = targetSize.width;
  73. sz.height = targetSize.width / originRatio;
  74. }
  75. }
  76. sz.width /= scale;
  77. sz.height /= scale;
  78. UIGraphicsBeginImageContextWithOptions(sz, NO, UIScreen.mainScreen.scale);
  79. [image drawInRect:CGRectMake(0, 0, sz.width, sz.height)];
  80. UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
  81. UIGraphicsEndImageContext();
  82. return newImage;
  83. }
  84. + (void)createNewImageFrom:(NSString *)fileName ocId:(NSString *)ocId etag:(NSString *)etag typeFile:(NSString *)typeFile
  85. {
  86. UIImage *originalImage;
  87. UIImage *scaleImagePreview;
  88. UIImage *scaleImageIcon;
  89. NSString *fileNamePath = [CCUtility getDirectoryProviderStorageOcId:ocId fileNameView:fileName];
  90. NSString *fileNamePathPreview = [CCUtility getDirectoryProviderStoragePreviewOcId:ocId etag:etag];
  91. NSString *fileNamePathIcon = [CCUtility getDirectoryProviderStorageIconOcId:ocId etag:etag];
  92. if (![CCUtility fileProviderStorageExists:ocId fileNameView:fileName]) return;
  93. // only viedo / image
  94. if (![typeFile isEqualToString: NCBrandGlobal.shared.metadataTypeFileImage] && ![typeFile isEqualToString: NCBrandGlobal.shared.metadataTypeFileVideo]) return;
  95. if ([typeFile isEqualToString: NCBrandGlobal.shared.metadataTypeFileImage]) {
  96. originalImage = [UIImage imageWithContentsOfFile:fileNamePath];
  97. if (originalImage == nil) { return; }
  98. }
  99. if ([typeFile isEqualToString: NCBrandGlobal.shared.metadataTypeFileVideo]) {
  100. // create symbolik link for read video file in temp
  101. [[NSFileManager defaultManager] removeItemAtPath:[NSTemporaryDirectory() stringByAppendingString:@"tempvideo.mp4"] error:nil];
  102. [[NSFileManager defaultManager] linkItemAtPath:fileNamePath toPath:[NSTemporaryDirectory() stringByAppendingString:@"tempvideo.mp4"] error:nil];
  103. originalImage = [self generateImageFromVideo:[NSTemporaryDirectory() stringByAppendingString:@"tempvideo.mp4"]];
  104. }
  105. scaleImagePreview = [self scaleImage:originalImage toSize:CGSizeMake(NCBrandGlobal.shared.sizePreview, NCBrandGlobal.shared.sizePreview) isAspectRation:YES];
  106. scaleImageIcon = [self scaleImage:originalImage toSize:CGSizeMake(NCBrandGlobal.shared.sizeIcon, NCBrandGlobal.shared.sizeIcon) isAspectRation:YES];
  107. scaleImagePreview = [UIImage imageWithData:UIImageJPEGRepresentation(scaleImagePreview, 0.5f)];
  108. scaleImageIcon = [UIImage imageWithData:UIImageJPEGRepresentation(scaleImageIcon, 0.5f)];
  109. // it is request write photo ?
  110. if (scaleImagePreview && scaleImageIcon) {
  111. [UIImageJPEGRepresentation(scaleImagePreview, 0.5) writeToFile:fileNamePathPreview atomically:true];
  112. [UIImageJPEGRepresentation(scaleImageIcon, 0.5) writeToFile:fileNamePathIcon atomically:true];
  113. }
  114. return;
  115. }
  116. + (UIColor *)colorFromHexString:(NSString *)hexString
  117. {
  118. unsigned rgbValue = 0;
  119. NSScanner *scanner = [NSScanner scannerWithString:hexString];
  120. [scanner setScanLocation:1]; // bypass '#' character
  121. [scanner scanHexInt:&rgbValue];
  122. return [UIColor colorWithRed:((rgbValue & 0xFF0000) >> 16)/255.0 green:((rgbValue & 0xFF00) >> 8)/255.0 blue:(rgbValue & 0xFF)/255.0 alpha:1.0];
  123. }
  124. + (UIImage *)changeThemingColorImage:(UIImage *)image multiplier:(NSInteger)multiplier color:(UIColor *)color
  125. {
  126. CGRect rect = CGRectMake(0, 0, image.size.width*multiplier / (2 / UIScreen.mainScreen.scale), image.size.height*multiplier / (2 / UIScreen.mainScreen.scale));
  127. UIGraphicsBeginImageContext(rect.size);
  128. CGContextRef context = UIGraphicsGetCurrentContext();
  129. CGContextClipToMask(context, rect, image.CGImage);
  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. + (UIImage *)changeThemingColorImage:(UIImage *)image width:(CGFloat)width height:(CGFloat)height color:(UIColor *)color
  137. {
  138. CGRect rect = CGRectMake(0, 0, width / (2 / UIScreen.mainScreen.scale), height / (2 / UIScreen.mainScreen.scale));
  139. UIGraphicsBeginImageContext(rect.size);
  140. CGContextRef context = UIGraphicsGetCurrentContext();
  141. CGContextClipToMask(context, rect, image.CGImage);
  142. if (color)
  143. CGContextSetFillColorWithColor(context, [color CGColor]);
  144. CGContextFillRect(context, rect);
  145. UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
  146. UIGraphicsEndImageContext();
  147. return [UIImage imageWithCGImage:img.CGImage scale:UIScreen.mainScreen.scale orientation: UIImageOrientationDownMirrored];
  148. }
  149. + (UIImage*)drawText:(NSString*)text inImage:(UIImage*)image colorText:(UIColor *)colorText sizeOfFont:(CGFloat)sizeOfFont
  150. {
  151. NSDictionary* attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:sizeOfFont], NSForegroundColorAttributeName:colorText};
  152. NSAttributedString* attributedString = [[NSAttributedString alloc] initWithString:text attributes:attributes];
  153. int x = image.size.width/2 - attributedString.size.width/2;
  154. int y = image.size.height/2 - attributedString.size.height/2;
  155. UIGraphicsBeginImageContext(image.size);
  156. [image drawInRect:CGRectMake(0,0,image.size.width,image.size.height)];
  157. CGRect rect = CGRectMake(x, y, image.size.width, image.size.height);
  158. [[UIColor whiteColor] set];
  159. [text drawInRect:CGRectIntegral(rect) withAttributes:attributes];
  160. UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
  161. newImage = [UIImage imageWithCGImage:newImage.CGImage scale:2 orientation:UIImageOrientationUp];
  162. UIGraphicsEndImageContext();
  163. return newImage;
  164. }
  165. // ------------------------------------------------------------------------------------------------------
  166. // MARK: Blur Image
  167. // ------------------------------------------------------------------------------------------------------
  168. + (UIImage *)blurryImage:(UIImage *)image withBlurLevel:(CGFloat)blur toSize:(CGSize)toSize
  169. {
  170. CIImage *inputImage = [CIImage imageWithCGImage:image.CGImage];
  171. CIFilter *filter = [CIFilter filterWithName:@"CIGaussianBlur" keysAndValues:kCIInputImageKey, inputImage, @"inputRadius", @(blur), nil];
  172. CIImage *outputImage = filter.outputImage;
  173. CIContext *context = [CIContext contextWithOptions:nil];
  174. CGImageRef outImage = [context createCGImage:outputImage fromRect:[outputImage extent]];
  175. UIImage *blurImage = [UIImage imageWithCGImage:outImage];
  176. return [CCGraphics scaleImage:blurImage toSize:toSize isAspectRation:YES];
  177. }
  178. // ------------------------------------------------------------------------------------------------------
  179. // MARK: Is Light Color
  180. // ------------------------------------------------------------------------------------------------------
  181. /*
  182. Color visibility can be determined according to the following algorithm:
  183. (This is a suggested algorithm that is still open to change.)
  184. Two colors provide good color visibility if the brightness difference and the color difference between the two colors are greater than a set range.
  185. Color brightness is determined by the following formula:
  186. ((Red value X 299) + (Green value X 587) + (Blue value X 114)) / 1000
  187. Note: This algorithm is taken from a formula for converting RGB values to YIQ values. This brightness value gives a perceived brightness for a color.
  188. Color difference is determined by the following formula:
  189. (maximum (Red value 1, Red value 2) - minimum (Red value 1, Red value 2)) + (maximum (Green value 1, Green value 2) - minimum (Green value 1, Green value 2)) + (maximum (Blue value 1, Blue value 2) - minimum (Blue value 1, Blue value 2))
  190. */
  191. + (BOOL)isLight:(UIColor *)color
  192. {
  193. const CGFloat *componentColors = CGColorGetComponents(color.CGColor);
  194. CGFloat colorBrightness = ((componentColors[0] * 299) + (componentColors[1] * 587) + (componentColors[2] * 114)) / 1000;
  195. if (colorBrightness < 0.8) { // STD 0.5
  196. return false;
  197. } else {
  198. return true;
  199. }
  200. }
  201. + (UIImage *)grayscale:(UIImage *)sourceImage
  202. {
  203. /* const UInt8 luminance = (red * 0.2126) + (green * 0.7152) + (blue * 0.0722); // Good luminance value */
  204. /// Create a gray bitmap context
  205. const size_t width = (size_t)sourceImage.size.width;
  206. const size_t height = (size_t)sourceImage.size.height;
  207. const int kNyxNumberOfComponentsPerGreyPixel = 3;
  208. CGRect imageRect = CGRectMake(0, 0, sourceImage.size.width, sourceImage.size.height);
  209. CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
  210. CGContextRef bmContext = CGBitmapContextCreate(NULL, width, height, 8/*Bits per component*/, width * kNyxNumberOfComponentsPerGreyPixel, colorSpace, kCGImageAlphaNone);
  211. CGColorSpaceRelease(colorSpace);
  212. if (!bmContext)
  213. return nil;
  214. /// Image quality
  215. CGContextSetShouldAntialias(bmContext, false);
  216. CGContextSetInterpolationQuality(bmContext, kCGInterpolationHigh);
  217. /// Draw the image in the bitmap context
  218. CGContextDrawImage(bmContext, imageRect, sourceImage.CGImage);
  219. /// Create an image object from the context
  220. CGImageRef grayscaledImageRef = CGBitmapContextCreateImage(bmContext);
  221. UIImage *grayscaled = [UIImage imageWithCGImage:grayscaledImageRef scale:sourceImage.scale orientation:sourceImage.imageOrientation];
  222. /// Cleanup
  223. CGImageRelease(grayscaledImageRef);
  224. CGContextRelease(bmContext);
  225. return grayscaled;
  226. }
  227. + (UIImage *)generateSinglePixelImageWithColor:(UIColor *)color
  228. {
  229. CGSize imageSize = CGSizeMake(1.0f, 1.0f);
  230. UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0.0f);
  231. CGContextRef theContext = UIGraphicsGetCurrentContext();
  232. CGContextSetFillColorWithColor(theContext, color.CGColor);
  233. CGContextFillRect(theContext, CGRectMake(0.0f, 0.0f, imageSize.width, imageSize.height));
  234. CGImageRef theCGImage = CGBitmapContextCreateImage(theContext);
  235. UIImage *theImage;
  236. if ([[UIImage class] respondsToSelector:@selector(imageWithCGImage:scale:orientation:)]) {
  237. theImage = [UIImage imageWithCGImage:theCGImage scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp];
  238. } else {
  239. theImage = [UIImage imageWithCGImage:theCGImage];
  240. }
  241. CGImageRelease(theCGImage);
  242. return theImage;
  243. }
  244. + (void)addImageToTitle:(NSString *)title colorTitle:(UIColor *)colorTitle imageTitle:(UIImage *)imageTitle imageRight:(BOOL)imageRight navigationItem:(UINavigationItem *)navigationItem
  245. {
  246. UIView *navView = [UIView new];
  247. UILabel *label = [UILabel new];
  248. if (imageRight)
  249. title = [NSString stringWithFormat:@" %@", title];
  250. label.text = title;
  251. [label sizeToFit];
  252. label.center = navView.center;
  253. label.textColor = colorTitle;
  254. label.textAlignment = NSTextAlignmentCenter;
  255. CGFloat correct = 6;
  256. UIImageView *image = [UIImageView new];
  257. image.image = imageTitle;
  258. CGFloat imageAspect = image.image.size.width/image.image.size.height;
  259. if (imageRight) {
  260. image.frame = CGRectMake(label.intrinsicContentSize.width+label.frame.origin.x+correct, label.frame.origin.y+correct/2, label.frame.size.height*imageAspect-correct, label.frame.size.height-correct);
  261. } else {
  262. image.frame = CGRectMake(label.frame.origin.x-label.frame.size.height*imageAspect, label.frame.origin.y+correct/2, label.frame.size.height*imageAspect-correct, label.frame.size.height-correct);
  263. }
  264. image.contentMode = UIViewContentModeScaleAspectFit;
  265. [navView addSubview:label];
  266. [navView addSubview:image];
  267. navigationItem.titleView = navView;
  268. [navView sizeToFit];
  269. }
  270. + (void)settingThemingColor:(NSString *)themingColor themingColorElement:(NSString *)themingColorElement themingColorText:(NSString *)themingColorText
  271. {
  272. UIColor *newColor, *newColorElement, *newColorText;
  273. // COLOR
  274. if (themingColor.length == 7) {
  275. newColor = [CCGraphics colorFromHexString:themingColor];
  276. } else {
  277. newColor = NCBrandColor.shared.customer;
  278. }
  279. // COLOR TEXT
  280. if (themingColorText.length == 7) {
  281. newColorText = [CCGraphics colorFromHexString:themingColorText];
  282. } else {
  283. newColorText = NCBrandColor.shared.customerText;
  284. }
  285. // COLOR ELEMENT
  286. if (themingColorElement.length == 7) {
  287. newColorElement = [CCGraphics colorFromHexString:themingColorElement];
  288. } else {
  289. if ([themingColorText isEqualToString:@"#000000"])
  290. newColorElement = [UIColor blackColor];
  291. else
  292. newColorElement = newColor;
  293. }
  294. NCBrandColor.shared.brand = newColor;
  295. NCBrandColor.shared.brandElement = newColorElement;
  296. NCBrandColor.shared.brandText = newColorText;
  297. }
  298. @end
  299. // ------------------------------------------------------------------------------------------------------
  300. // MARK: Avatar
  301. // ------------------------------------------------------------------------------------------------------
  302. @implementation CCAvatar
  303. - (id)initWithImage:(UIImage *)image borderColor:(UIColor*)borderColor borderWidth:(float)borderWidth
  304. {
  305. self = [super initWithImage:image];
  306. float cornerRadius = self.frame.size.height/2.0f;
  307. CALayer *layer = [self layer];
  308. [layer setMasksToBounds:YES];
  309. [layer setCornerRadius: cornerRadius];
  310. [layer setBorderWidth: borderWidth];
  311. [layer setBorderColor:[borderColor CGColor]];
  312. return self;
  313. }
  314. @end