2011-09-20 47 views
2

我需要在NSTextView中調整NSImage的大小。而我做到了,但是當我嘗試在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]; 
} 

它是委託的NSTextView方法,而且比我調整IMGE在resizeImageController的方法 - (無效)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]]; 
} 

回答

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; 
} 
相關問題