TOActivityCroppedImageProvider.m 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. //
  2. // TOActivityCroppedImageProvider.m
  3. //
  4. // Copyright 2015 Timothy Oliver. All rights reserved.
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to
  8. // deal in the Software without restriction, including without limitation the
  9. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  10. // sell copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  17. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  20. // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
  21. // IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. #import "TOActivityCroppedImageProvider.h"
  23. #import "UIImage+CropRotate.h"
  24. @interface TOActivityCroppedImageProvider ()
  25. @property (nonatomic, strong, readwrite) UIImage *image;
  26. @property (nonatomic, assign, readwrite) CGRect cropFrame;
  27. @property (nonatomic, assign, readwrite) NSInteger angle;
  28. @property (atomic, strong) UIImage *croppedImage;
  29. @end
  30. @implementation TOActivityCroppedImageProvider
  31. - (instancetype)initWithImage:(UIImage *)image cropFrame:(CGRect)cropFrame angle:(NSInteger)angle
  32. {
  33. if (self = [super initWithPlaceholderItem:[UIImage new]]) {
  34. _image = image;
  35. _cropFrame = cropFrame;
  36. _angle = angle;
  37. }
  38. return self;
  39. }
  40. #pragma mark - UIActivity Protocols -
  41. - (id)activityViewControllerPlaceholderItem:(UIActivityViewController *)activityViewController
  42. {
  43. return [[UIImage alloc] init];
  44. }
  45. - (id)activityViewController:(UIActivityViewController *)activityViewController itemForActivityType:(NSString *)activityType
  46. {
  47. return self.croppedImage;
  48. }
  49. #pragma mark - Image Generation -
  50. - (id)item
  51. {
  52. //If the user didn't touch the image, just forward along the original
  53. if (self.angle == 0 && CGRectEqualToRect(self.cropFrame, (CGRect){CGPointZero, self.image.size})) {
  54. self.croppedImage = self.image;
  55. return self.croppedImage;
  56. }
  57. UIImage *image = [self.image croppedImageWithFrame:self.cropFrame angle:self.angle];
  58. self.croppedImage = image;
  59. return self.croppedImage;
  60. }
  61. @end