CCGraphics.m 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. + (void)createNewImageFrom:(NSString *)fileName ocId:(NSString *)ocId etag:(NSString *)etag typeFile:(NSString *)typeFile
  59. {
  60. UIImage *originalImage;
  61. UIImage *scaleImagePreview;
  62. UIImage *scaleImageIcon;
  63. NSString *fileNamePath = [CCUtility getDirectoryProviderStorageOcId:ocId fileNameView:fileName];
  64. NSString *fileNamePathPreview = [CCUtility getDirectoryProviderStoragePreviewOcId:ocId etag:etag];
  65. NSString *fileNamePathIcon = [CCUtility getDirectoryProviderStorageIconOcId:ocId etag:etag];
  66. if (![CCUtility fileProviderStorageExists:ocId fileNameView:fileName]) return;
  67. // only viedo / image
  68. if (![typeFile isEqualToString: NCBrandGlobal.shared.metadataTypeFileImage] && ![typeFile isEqualToString: NCBrandGlobal.shared.metadataTypeFileVideo]) return;
  69. if ([typeFile isEqualToString: NCBrandGlobal.shared.metadataTypeFileImage]) {
  70. originalImage = [UIImage imageWithContentsOfFile:fileNamePath];
  71. if (originalImage == nil) { return; }
  72. }
  73. if ([typeFile isEqualToString: NCBrandGlobal.shared.metadataTypeFileVideo]) {
  74. // create symbolik link for read video file in temp
  75. [[NSFileManager defaultManager] removeItemAtPath:[NSTemporaryDirectory() stringByAppendingString:@"tempvideo.mp4"] error:nil];
  76. [[NSFileManager defaultManager] linkItemAtPath:fileNamePath toPath:[NSTemporaryDirectory() stringByAppendingString:@"tempvideo.mp4"] error:nil];
  77. originalImage = [self generateImageFromVideo:[NSTemporaryDirectory() stringByAppendingString:@"tempvideo.mp4"]];
  78. }
  79. scaleImagePreview = [NCUtility.shared resizeImage:originalImage size:CGSizeMake(NCBrandGlobal.shared.sizePreview, NCBrandGlobal.shared.sizePreview)];
  80. scaleImageIcon = [NCUtility.shared resizeImage:originalImage size:CGSizeMake(NCBrandGlobal.shared.sizeIcon, NCBrandGlobal.shared.sizeIcon)];
  81. scaleImagePreview = [UIImage imageWithData:UIImageJPEGRepresentation(scaleImagePreview, 0.5f)];
  82. scaleImageIcon = [UIImage imageWithData:UIImageJPEGRepresentation(scaleImageIcon, 0.5f)];
  83. // it is request write photo ?
  84. if (scaleImagePreview && scaleImageIcon) {
  85. [UIImageJPEGRepresentation(scaleImagePreview, 0.5) writeToFile:fileNamePathPreview atomically:true];
  86. [UIImageJPEGRepresentation(scaleImageIcon, 0.5) writeToFile:fileNamePathIcon atomically:true];
  87. }
  88. return;
  89. }
  90. + (UIColor *)colorFromHexString:(NSString *)hexString
  91. {
  92. unsigned rgbValue = 0;
  93. NSScanner *scanner = [NSScanner scannerWithString:hexString];
  94. [scanner setScanLocation:1]; // bypass '#' character
  95. [scanner scanHexInt:&rgbValue];
  96. return [UIColor colorWithRed:((rgbValue & 0xFF0000) >> 16)/255.0 green:((rgbValue & 0xFF00) >> 8)/255.0 blue:(rgbValue & 0xFF)/255.0 alpha:1.0];
  97. }
  98. + (void)settingThemingColor:(NSString *)themingColor themingColorElement:(NSString *)themingColorElement themingColorText:(NSString *)themingColorText
  99. {
  100. UIColor *newColor, *newColorElement, *newColorText;
  101. // COLOR
  102. if (themingColor.length == 7) {
  103. newColor = [CCGraphics colorFromHexString:themingColor];
  104. } else {
  105. newColor = NCBrandColor.shared.customer;
  106. }
  107. // COLOR TEXT
  108. if (themingColorText.length == 7) {
  109. newColorText = [CCGraphics colorFromHexString:themingColorText];
  110. } else {
  111. newColorText = NCBrandColor.shared.customerText;
  112. }
  113. // COLOR ELEMENT
  114. if (themingColorElement.length == 7) {
  115. newColorElement = [CCGraphics colorFromHexString:themingColorElement];
  116. } else {
  117. if ([themingColorText isEqualToString:@"#000000"])
  118. newColorElement = [UIColor blackColor];
  119. else
  120. newColorElement = newColor;
  121. }
  122. NCBrandColor.shared.brand = newColor;
  123. NCBrandColor.shared.brandElement = newColorElement;
  124. NCBrandColor.shared.brandText = newColorText;
  125. }
  126. @end