3
votes

I need to resize NSImage in NSTextView. And i did it, but when i try change place of image (my NSImage) in the NSTextView - then my Image gets it's old size. Can someone help to me? Here is code, that i use:

- (void)textView:(NSTextView *)textView doubleClickedOnCell:(id <NSTextAttachmentCell>)cell inRect:(NSRect)cellFrame atIndex:(NSUInteger)charIndex {

NSImage * image = [(NSCell *)cell image];

NSSize imageSize = [image size];

self.resizeImageController.sizeBefore = imageSize;
self.resizeImageController.imageForResize = image;

self.resizeImageController.textViewWithImage = textView;
self.resizeImageController.textAttachmentCell = cell;

[[self.resizeImageController window]orderFront:self];
}

It was delegate's of NSTextView method, and than i resize Imge in resizeImageController in method - (void)resizeImage; :

- (void)resizeImage {

NSSize newSize = ...;//Get new image size - the dimensions are correct, the error is not exactly here

[self.imageForResize setSize:newSize];

NSImage *newImage = [[[NSImage alloc] initWithSize:newSize] autorelease];
NSBitmapImageRep *rep = [[[NSBitmapImageRep alloc]
                         initWithData:[self.imageForResize TIFFRepresentation]] autorelease];
[rep setSize:newSize];
[newImage addRepresentation:rep];
[self.textAttachmentCell setImage:newImage];   
self.imageForResize = newImage;
[[self.textViewWithImage layoutManager] textContainerChangedGeometry: [self.textViewWithImage textContainer]];
}
1

1 Answers

0
votes

I won this problem. It was in NSFileWrapper - he kept a reference to the old data of image. Now I use the following methods to resize pictures:

- (void)textView:(NSTextView *)textView doubleClickedOnCell:(id <NSTextAttachmentCell>)cell inRect:(NSRect)cellFrame atIndex:(NSUInteger)charIndex;// - Use as previously

- (void)resizeImage {

    NSSize newSize = ...;//Get new image size - the dimensions are correct, the error is not exactly here
    self.newImage = [self imageResize: self.imageForResize newSize:newSize];
    NSFileWrapper *fileWrapper = [[NSFileWrapper alloc]
                              initRegularFileWithContents:[self.newImage
                                                               TIFFRepresentationUsingCompression:NSTIFFCompressionLZW factor:1]];

    NSTextStorage *test = [[[NSTextStorage alloc] initWithAttributedString: [self.textViewWithImage attributedString]] autorelease];

    [fileWrapper setPreferredFilename:@"image.tiff"];
    NSTextAttachment *attachment = [[[NSTextAttachment alloc]
                                 initWithFileWrapper:fileWrapper] autorelease];
    NSAttributedString *string = [NSAttributedString
                              attributedStringWithAttachment:attachment];

    [test replaceCharactersInRange: 
    NSMakeRange(self.charIndex, [string length]) withAttributedString:string];

    [[self.textViewWithImage textStorage] setAttributedString: test];
}

- (NSImage *)imageResize:(NSImage*)anImage
             newSize:(NSSize)newSize {
    NSImage *sourceImage = anImage;
    [sourceImage setScalesWhenResized:YES];

    // Report an error if the source isn't a valid image
    if (![sourceImage isValid])
    {
        NSLog(@"Invalid Image");
    } else
    {
        NSImage *smallImage = [[[NSImage alloc] initWithSize: newSize] autorelease];
        [smallImage lockFocus];
        [sourceImage setSize: newSize];
        [[NSGraphicsContext currentContext] setImageInterpolation:NSImageInterpolationHigh];
        [sourceImage compositeToPoint:NSZeroPoint operation:NSCompositeCopy];
        [smallImage unlockFocus];
        return smallImage;
    }
    return nil;
}