MMRichImageModel.m 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //
  2. // MMRichImageModel.m
  3. // RichTextEditDemo
  4. //
  5. // Created by aron on 2017/7/19.
  6. // Copyright © 2017年 aron. All rights reserved.
  7. //
  8. #import "MMRichImageModel.h"
  9. #import "MMRichTextConfig.h"
  10. #import "MMRichContentUtil.h"
  11. #import "UIImage+Util.h"
  12. @interface MMRichImageModel ()
  13. @property (nonatomic, strong) NSAttributedString* attrString;
  14. @property (nonatomic, assign) CGRect imageFrame;
  15. @end
  16. @implementation MMRichImageModel
  17. - (instancetype)init
  18. {
  19. self = [super init];
  20. if (self) {
  21. self.richContentType = MMRichContentTypeImage;
  22. }
  23. return self;
  24. }
  25. + (nullable NSArray<NSString *> *)modelPropertyWhitelist {
  26. return @[@"localImageName", @"remoteImageUrlString", @"imageContentHeight", @"uploadProgress", @"isFailed", @"isDone", @"richContentType"];
  27. }
  28. /**
  29. 显示图片的属性文字
  30. */
  31. - (NSAttributedString*)attrStringWithContainerWidth:(NSInteger)containerWidth {
  32. if (!_attrString) {
  33. CGFloat showImageWidth = containerWidth - MMEditConfig.editAreaLeftPadding - MMEditConfig.editAreaRightPadding - MMEditConfig.imageDeltaWidth;
  34. CGFloat showImageHeight = showImageWidth * self.image.size.height / self.image.size.width;
  35. NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
  36. CGRect rect = CGRectZero;
  37. rect.size.width = showImageWidth;
  38. rect.size.height = showImageHeight;
  39. textAttachment.bounds = rect;
  40. textAttachment.image = [UIImage new];
  41. NSAttributedString *attachmentString = [NSAttributedString attributedStringWithAttachment:textAttachment];
  42. NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@""];
  43. [attributedString insertAttributedString:attachmentString atIndex:0];
  44. _attrString = attributedString;
  45. // 设置Height
  46. if (_imageContentHeight <= 0) {
  47. _imageContentHeight = MAX(rect.size.height + MMEditConfig.editAreaTopPadding + MMEditConfig.editAreaBottomPadding, MMEditConfig.minImageContentCellHeight);
  48. }
  49. // 设置Image的Frame
  50. _imageFrame = CGRectMake(MMEditConfig.editAreaLeftPadding + MMEditConfig.imageDeltaWidth / 2, MMEditConfig.editAreaTopPadding, showImageWidth, showImageHeight);
  51. }
  52. return _attrString;
  53. }
  54. - (UIImage *)image {
  55. if (!_image) {
  56. _image = [UIImage imageWithContentsOfFile:[[MMRichContentUtil imageSavedLocalPath] stringByAppendingPathComponent:self.localImageName]];
  57. }
  58. return _image;
  59. }
  60. - (void)setUploadProgress:(float)uploadProgress {
  61. _uploadProgress = uploadProgress;
  62. if ([_uploadDelegate respondsToSelector:@selector(uploadProgress:)]) {
  63. [_uploadDelegate uploadProgress:uploadProgress];
  64. }
  65. }
  66. - (void)setIsDone:(BOOL)isDone {
  67. _isDone = isDone;
  68. if ([_uploadDelegate respondsToSelector:@selector(uploadDone)]) {
  69. [_uploadDelegate uploadDone];
  70. }
  71. }
  72. - (void)setIsFailed:(BOOL)isFailed {
  73. _isFailed = isFailed;
  74. if ([_uploadDelegate respondsToSelector:@selector(uploadFail)]) {
  75. [_uploadDelegate uploadFail];
  76. }
  77. }
  78. #pragma mark - ......::::::: UploadItemCallBackProtocal :::::::......
  79. - (void)mm_uploadProgress:(float)progress {
  80. self.uploadProgress = progress;
  81. }
  82. - (void)mm_uploadFailed {
  83. self.isFailed = YES;
  84. }
  85. - (void)mm_uploadDone:(NSString *)remoteImageUrlString {
  86. self.remoteImageUrlString = remoteImageUrlString;
  87. self.isDone = YES;
  88. }
  89. #pragma mark - ......::::::: UploadItemProtocal :::::::......
  90. - (NSData*)mm_uploadData {
  91. return UIImageJPEGRepresentation(_image, 0.5);
  92. }
  93. - (NSURL*)mm_uploadFileURL {
  94. return nil;
  95. }
  96. @end