0

I use UITextView as the rich text view

- (void)openPhotoLibrary {
   [self presentImagePickerController:UIImagePickerControllerSourceTypePhotoLibrary];
}

- (void)presentImagePickerController:(UIImagePickerControllerSourceType)sourceType {
   UIImagePickerController *picker = [[UIImagePickerController alloc] init];
   picker.delegate = self;
   picker.sourceType = sourceType;
   [self presentViewController:picker animated:YES completion:nil];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
[picker dismissViewControllerAnimated:YES completion:nil];
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
NSData *dataImage = UIImageJPEGRepresentation(image, 0.1);
UIImage *resultImage = [UIImage imageWithData:dataImage];
[self insertImageOfTheTextView:self.textview withImage:resultImage];
[picker dismissViewControllerAnimated:YES completion:nil];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
  [picker dismissViewControllerAnimated:YES completion:nil];
}

- (void)insertImageOfTheTextView:(UITextView *)textView withImage:(UIImage *)image {
 if (image == nil || ![image isKindOfClass:[UIImage class]]) {
    return;
 }
NImageAttachment *attachment = [[NImageAttachment alloc] init];
attachment.image = image;
attachment.imageSize = [self scaleImageSize:image];

NSAttributedString *imageAttributedString = [self insertImageDoneAutoReturn:[NSAttributedString attributedStringWithAttachment:attachment]];
[textView.textStorage insertAttributedString:imageAttributedString atIndex:textView.selectedRange.location];
textView.selectedRange = NSMakeRange(textView.selectedRange.location + 3, 0);
}

- (CGSize)scaleImageSize:(UIImage *)image {
   CGFloat imageScale = image.size.width / image.size.height;
   CGFloat imageWidth = self.view.frame.size.width;
   CGSize imageSize = CGSizeMake(imageWidth, imageWidth / imageScale);
   return imageSize;
}

- (NSAttributedString *)insertImageDoneAutoReturn:(NSAttributedString *)imageAttributedString {
  NSAttributedString *returnAttributedString = [[NSAttributedString alloc] initWithString:@"\n"];
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithAttributedString:imageAttributedString];
[attributedString appendAttributedString:returnAttributedString];
[attributedString insertAttributedString:returnAttributedString atIndex:0];
return attributedString;
}

If the photo I picked from PhotoLibrary is small, the uitextivew will be fine, if the photo is large, the uitextview will be slow when I scroll or edit the uitextview, it seems stucks, how can I optimize it?

  • 1
    Rescale the `UIImage`, not only the `imageSize`? – Larme Jan 08 '18 at 15:42
  • @Larme can you give me more detail infos? –  Jan 08 '18 at 15:48
  • https://stackoverflow.com/questions/7645454/resize-uiimage-by-keeping-aspect-ratio-and-width ? I think that your image is too big (in data, not necessarly in points size). – Larme Jan 08 '18 at 15:50
  • @Larme thanks, I will try. –  Jan 08 '18 at 16:14
  • I solved this question, the image should be smaller that is the key. In the above code I only compress it, but it still big, rescale it, and then compress, it will be fine. –  Jan 09 '18 at 03:00

0 Answers0