2
0

ShareUtility.m 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. //
  2. // ShareUtility.m
  3. // ShareExtension
  4. //
  5. // Created by Tom Lee on 2020/10/8.
  6. // Copyright © 2020 WildFireChat. All rights reserved.
  7. //
  8. #import "ShareUtility.h"
  9. #import "SharePredefine.h"
  10. @implementation ShareUtility
  11. + (CGSize)imageScaleSize:(CGSize)imageSize targetSize:(CGSize)targetSize thumbnailPoint:(CGPoint *)thumbnailPoint {
  12. if (imageSize.width == 0 && imageSize.height == 0) {
  13. return targetSize;
  14. }
  15. CGFloat imageWidth = imageSize.width;
  16. CGFloat imageHeight = imageSize.height;
  17. CGFloat targetWidth = targetSize.width;
  18. CGFloat targetHeight = targetSize.height;
  19. CGFloat scaleFactor = 0.0;
  20. CGFloat scaledWidth = 0.0;
  21. CGFloat scaledHeight = 0.0;
  22. if (imageWidth/imageHeight < 2.4 && imageHeight/imageWidth < 2.4) {
  23. CGFloat widthFactor = targetWidth / imageWidth;
  24. CGFloat heightFactor = targetHeight / imageHeight;
  25. if (widthFactor < heightFactor)
  26. scaleFactor = widthFactor;
  27. else
  28. scaleFactor = heightFactor;
  29. scaledWidth = imageWidth * scaleFactor;
  30. scaledHeight = imageHeight * scaleFactor;
  31. if (widthFactor > heightFactor) {
  32. if (thumbnailPoint) {
  33. thumbnailPoint->y = (targetHeight - scaledHeight) * 0.5;
  34. }
  35. } else if (widthFactor < heightFactor) {
  36. if (thumbnailPoint) {
  37. thumbnailPoint->x = (targetWidth - scaledWidth) * 0.5;
  38. }
  39. }
  40. } else {
  41. if(imageWidth/imageHeight > 2.4) {
  42. scaleFactor = 100 * targetHeight / imageHeight / 240;
  43. } else {
  44. scaleFactor = 100 * targetWidth / imageWidth / 240;
  45. }
  46. scaledWidth = imageWidth * scaleFactor;
  47. scaledHeight = imageHeight * scaleFactor;
  48. }
  49. return CGSizeMake(scaledWidth, scaledHeight);
  50. }
  51. + (UIImage *)generateThumbnail:(UIImage *)image
  52. withWidth:(CGFloat)targetWidth
  53. withHeight:(CGFloat)targetHeight {
  54. UIImage *targetImage = nil;
  55. CGPoint thumbnailPoint = CGPointMake(0.0, 0.0);
  56. CGSize targetSize = [ShareUtility imageScaleSize:image.size targetSize:CGSizeMake(targetWidth, targetHeight) thumbnailPoint:&thumbnailPoint];
  57. CGFloat scaledWidth = targetSize.width;
  58. CGFloat scaledHeight = targetSize.height;
  59. CGFloat imageWidth = image.size.width;
  60. CGFloat imageHeight = image.size.height;
  61. UIGraphicsBeginImageContext(CGSizeMake(scaledWidth, scaledHeight));
  62. CGRect thumbnailRect = CGRectZero;
  63. thumbnailRect.origin = thumbnailPoint;
  64. thumbnailRect.size.width = scaledWidth;
  65. thumbnailRect.size.height = scaledHeight;
  66. [image drawInRect:thumbnailRect];
  67. targetImage = UIGraphicsGetImageFromCurrentImageContext();
  68. if(imageWidth/imageHeight > 2.4) {
  69. CGRect rect = CGRectZero;
  70. rect.origin.x = ( targetImage.size.width - 240)/2;
  71. rect.size.width = 240;
  72. rect.origin.y = 0;
  73. rect.size.height = targetImage.size.height;
  74. CGImageRef imageRef = CGImageCreateWithImageInRect([ targetImage CGImage], rect);
  75. targetImage = [UIImage imageWithCGImage:imageRef];
  76. CGImageRelease(imageRef);
  77. } else if(imageHeight/imageWidth > 2.4) {
  78. CGRect rect = CGRectZero;
  79. rect.origin.y = ( targetImage.size.height - 240)/2;
  80. rect.size.height = 240;
  81. rect.origin.x = 0;
  82. rect.size.width = targetImage.size.width;
  83. CGImageRef imageRef = CGImageCreateWithImageInRect([ targetImage CGImage], rect);
  84. targetImage = [UIImage imageWithCGImage:imageRef];
  85. CGImageRelease(imageRef);
  86. }
  87. if ( targetImage == nil)
  88. NSLog(@"could not scale image");
  89. UIGraphicsEndImageContext();
  90. return targetImage;
  91. }
  92. + (NSURL *)getSavedGroupGridPortrait:(NSString *)groupId {
  93. NSURL *groupURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:WFC_SHARE_APP_GROUP_ID];
  94. NSURL *portraitURL = [groupURL URLByAppendingPathComponent:WFC_SHARE_BACKUPED_GROUP_GRID_PORTRAIT_PATH];
  95. NSURL *fileURL = [portraitURL URLByAppendingPathComponent:groupId];
  96. return fileURL;
  97. }
  98. @end