Scaling images for Insertion into UITextView .attributedText
Scaling images for Insertion into UITextView .attributedText
- Subject: Scaling images for Insertion into UITextView .attributedText
- From: "Michal L. Wright" <email@hidden>
- Date: Wed, 15 Apr 2015 11:58:47 -0400
Hi,
I have an iOS-based flat-file database app that shows records in a UIViewController, called RecordViewController.
I’ve added an “Insert Photo” menu item to the editing menu. This brings up a photo picker view that sends the selected image back to RecordViewController.
I’ve included code to scale the image to fit the screen width, which seems to work fine when it first displays. However, if I save the datafile or just display a different record, then return to the record with the embedded image, the image is no longer scaled, but has reverted to its original size.
I haven’t done much with images (and Quartz is just another mineral to me), so I don't know what else I might need to do to keep the scaling from reverting.
Here’s the RecordViewController routine that is called to embed the image in the UITextView.
- (void)insertPhoto:(UIImage *)image
{
// determine the scaling factor to fit the screen width
CGFloat oldWidth = image.size.width;
NSUInteger newWidth = (g_textView.frame.size.width - 10);
CGFloat scaleFactor;
if (oldWidth > newWidth)
scaleFactor = oldWidth / newWidth;
else
scaleFactor = 1;
// create scaled version of image
CGImageRef imageRef = image.CGImage;
UIImage *scaledImage = [UIImage imageWithCGImage:imageRef scale:scaleFactor orientation:UIImageOrientationUp];
// create a text attachment containing the scaled image
NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
textAttachment.image = scaledImage;
// create an attributed string containing the text attachment
NSAttributedString *attrStringWithImage = [NSAttributedString attributedStringWithAttachment:textAttachment];
// create a mutable attributed string, tempMutableAttributedText, from the text view's .attributedText
NSMutableAttributedString *tempMutableAttributedText = [[NSMutableAttributedString alloc] initWithAttributedString:g_textView.attributedText];
// insert the attributed string containing the image at the selection point in tempMutableAttributedText
NSRange insertionRange = g_textView.selectedRange;
[tempMutableAttributedText replaceCharactersInRange:insertionRange withAttributedString:attrStringWithImage];
// set the text view's .attributedText to tempMutableAttributedText
[g_textView setAttributedText:tempMutableAttributedText];
[[self currentRecord] setIsDirty:YES];
[self setDatafileDirty:YES];
}
So, what am I missing?
And, since different devices need different scaling, is there a way to scale multiple embedded images on the fly? If not, I may add code to the photo picker to let the user specify the scaling factor.
Thanks,
Mike Wright
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden