Objective c 在NSTextView中调整NSImage的大小

Objective c 在NSTextView中调整NSImage的大小,objective-c,cocoa,Objective C,Cocoa,我需要在NSTextView中调整NSImage的大小。我做到了,但当我尝试在NSTextView中更改图像的位置(我的NSImage)时,我的图像得到了原来的大小。有人能帮我吗?以下是我使用的代码: - (void)textView:(NSTextView *)textView doubleClickedOnCell:(id <NSTextAttachmentCell>)cell inRect:(NSRect)cellFrame atIndex:(NSUInteger)charIn

我需要在NSTextView中调整NSImage的大小。我做到了,但当我尝试在NSTextView中更改图像的位置(我的NSImage)时,我的图像得到了原来的大小。有人能帮我吗?以下是我使用的代码:

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

我赢了这个问题。它在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;
}
-(void)textView:(NSTextView*)textView双击取消选中:(NSRect)单元格框索引:(nsInteger)charIndex;//-如前所述使用
-(无效)调整图像大小{
NSSize newSize=…;//获取新图像大小-尺寸正确,错误不在此处
self.newImage=[self-imagesize:self.imagesforresize-newSize:newSize];
NSFileWrapper*fileWrapper=[[NSFileWrapper alloc]
initRegularFileWithContents:[self.newImage
TIFF表示使用压缩:NSTIFF压缩LZW系数:1]];
NSTextStorage*test=[[NSTextStorage alloc]initWithAttributedString:[self.textViewWithImage attributedString]]autorelease];
[fileWrapper setPreferredFilename:@“image.tiff”];
NSTextAttachment*附件=[[NSTextAttachment alloc]
initWithFileWrapper:fileWrapper]自动释放];
NSAttributedString*string=[NSAttributedString
attributedStringWithAttachment:附件];
[测试替换字符范围:
NSMakeRange(self.charIndex,[string length]),带AttributedString:string];
[[self.textViewWithImage textStorage]设置属性字符串:测试];
}
-(NSImage*)图像大小:(NSImage*)动画
新闻大小:(NSSize)新闻大小{
NSImage*sourceImage=anImage;
[sourceImage setScalesWhenResized:是];
//如果源不是有效的映像,请报告错误
如果(![sourceImage有效])
{
NSLog(@“无效映像”);
}否则
{
NSImage*smallImage=[[NSImage alloc]initWithSize:newSize]autorelease];
[小型图像锁定焦点];
[sourceImage setSize:newSize];
[[NSGraphicsContext currentContext]设置图像插值:NSImageInterpolationHigh];
[sourceImage compositeToPoint:NSZeroPoint操作:NSCompositeCopy];
[smallImage unlockFocus];
返回图像;
}
返回零;
}

我赢了这个问题。它在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;
}
-(void)textView:(NSTextView*)textView双击取消选中:(NSRect)单元格框索引:(nsInteger)charIndex;//-如前所述使用
-(无效)调整图像大小{
NSSize newSize=…;//获取新图像大小-尺寸正确,错误不在此处
self.newImage=[self-imagesize:self.imagesforresize-newSize:newSize];
NSFileWrapper*fileWrapper=[[NSFileWrapper alloc]
initRegularFileWithContents:[self.newImage
TIFF表示使用压缩:NSTIFF压缩LZW系数:1]];
NSTextStorage*test=[[NSTextStorage alloc]initWithAttributedString:[self.textViewWithImage attributedString]]autorelease];
[fileWrapper setPreferredFilename:@“image.tiff”];
NSTextAttachment*附件=[[NSTextAttachment alloc]
initWithFileWrapper:fileWrapper]自动释放];
NSAttributedString*string=[NSAttributedString
attributedStringWithAttachment:附件];
[测试替换字符范围:
NSMakeRange(self.charIndex,[string length]),带AttributedString:string];
[[self.textViewWithImage textStorage]设置属性字符串:测试];
}
-(NSImage*)图像大小:(NSImage*)动画
新闻大小:(NSSize)新闻大小{
NSImage*sourceImage=anImage;
[sourceImage setScalesWhenResized:是];
//如果源不是有效的映像,请报告错误
如果(![sourceImage有效])
{
NSLog(@“无效映像”);
}否则
{
NSImage*smallImage=[[NSImage alloc]initWithSize:newSize]autorelease];
[小型图像锁定焦点];
[sourceImage setSize:newSize];
[[NSGraphicsContext currentContext]设置图像插值:NSImageInterpolationHigh];
[sourceImage compositeToPoint:NSZeroPoint操作:NSCompositeCopy];
[smallImage unlockFocus];
返回图像;
}
返回零;
}