我需要在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方法的委托,而不是我在resizeImageController中重新调整Imge方法 - (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]];
}
答案 0 :(得分: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;
}