7
votes

I am building a note editor using the Text Kit in ios7. Earlier I had trouble in rendering of custom size NSTextAttachment's as it was slowing down the rendering to a great extent.I solved the issue by scaling the images and then adding them to textview.You can find my answer in iOS 7.0 UITextView gettings terribly slow after adding images to it After scaling the images the textview rendering runs fine without any lag.The attributed text of textview is stored in core data.During a running session of application the textview displays the images correctly.Even after saving the attributed text in the core data and retrieving it again to display on textview,the images look fine.But after killing the app and again running the application.The images get enlarged to 2x size.while scaling the images I used the following function and used [[UIScreen bounds] scale] to maintain the image quality.

- (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {

     UIGraphicsBeginImageContextWithOptions(newSize, NO, [UIScreen mainScreen].scale);
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

If I scale the images to 1.0 the images doesn't expand but the image quality is very bad.

What I Think where the problem lies? The problem lies in the layout manager.

What I have Tried I have tried subclassing the NSLayoutManager and overriding the - (void)drawGlyphsForGlyphRange:(NSRange)glyphsToShow atPoint:(CGPoint)origin What I see is the attachment size is doubling when running a new session of the application.If I try to check the size of attachment and resize it.The lag starts coming again. I am stucked with this problem from a quite time.Any suggestions would be much appreciated. enter image description hereenter image description here

2
How are you storing the image? Is it possible that as a result of the scaling you perform you are modifying the original image in the attachment and next time it gets scaled again. What happens when you run the app a third time ? How does the app know when to not scale the image ? Perhaps set some additional attribute in the image attachment to let the app know not to scale the image.Duncan Groenewald
1)I am storing the complete NSAttributedString inside the core data.nick28
2)Yes that might be the reason.The app behaves same on the third run or after 3)From what i get reading other questions is that the default NSTextAttachment takes the size of image as its bounds.This is which helped me in making a work around and solving itnick28
I am observing a lag if I am replacing the instances of NSTextattachment with the subclassed object.Have you observed something like that on iphone or is it with me onlynick28
Download iWalletFree and test it to see if you get the same lag.Duncan Groenewald

2 Answers

4
votes

Could the reason due to retina display? If it is retina, you might need to reduce the size by 50% before storing. How about trying this:-

 //Original Size that you want to store
CGSize imageSize = CGSizeMake(320.0f, 320.0f);

//Make the image 50% of the size for retina
if ([[UIScreen mainScreen] respondsToSelector:@selector(displayLinkWithTarget:selector:)] &&([UIScreen mainScreen].scale == 2.0)) {
    // Retina display
    imageSize = CGSizeMake(160.0f, 160.0f);
}

UIImage * storeImage = [self imageWithImage:self.image scaledToSize:imageSize]
//TODO: Store this image locally or whatever you want to do.
2
votes
@interface MMTextAttachment : NSTextAttachment
{
}
@end
@implementation MMTextAttachment
//I want my emoticon has the same size with line's height
- (CGRect)attachmentBoundsForTextContainer:(NSTextContainer *)textContainer proposedLineFragment:(CGRect)lineFrag glyphPosition:(CGPoint)position characterIndex:(NSUInteger)charIndex NS_AVAILABLE_IOS(7_0)
{
return CGRectMake( 0 , 0 , lineFrag.size.height , lineFrag.size.height );
}
@end

I think you can try this.