在NSTextView中调整NSImage大小

3

我需要在NSTextView中调整NSImage的大小。我已经成功实现了它,但是当我尝试改变图片(我的NSImage)在NSTextView中的位置时,图片将恢复到其旧尺寸。有人可以帮助我吗?以下是我的代码:

- (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];
}

这是NSTextView方法的代理,并且我在resizeImageController的方法 - (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个回答

0

我解决了这个问题。它与 NSFileWrapper 有关 - 它保留了图像旧数据的引用。现在我使用以下方法来调整图片大小:

- (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;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接