nsimage

NSTextView insert image in between text

﹥>﹥吖頭↗ 提交于 2019-11-27 13:23:44
问题 Is it possible to insert an image (not a background image) into an NSTextView ? Something like: Hi :) How are you? and it should display a "smiley" image. I have an NSTextView and an NSImage . 回答1: Insert NSImage into NSTextView: NSImage * pic = [[NSImage alloc] initWithContentsOfFile:@"/Users/Anne/Desktop/Sample.png"]; NSTextAttachmentCell *attachmentCell = [[NSTextAttachmentCell alloc] initImageCell:pic]; NSTextAttachment *attachment = [[NSTextAttachment alloc] init]; [attachment

How to resize NSImage

我是研究僧i 提交于 2019-11-27 12:26:44
I have NSBitmapImageRep which is W x H size. And I create NSImage and do addRepresentation: . Then I need to resize NSImage . I tried setSize method but it doesn't works. What should I do? Edit: Since this answer is still the accepted answer, but was written without Retina screens in mind, I will straight up link to a better solution further down the thread: Objective-C Swift 4 Because the method of Paresh is totally correct but deprecated since 10.8 I'll post the working 10.8 code below. All credit to Paresh's answer though. - (NSImage *)imageResize:(NSImage*)anImage newSize:(NSSize)newSize {

NSImage size not real size with some pictures?

混江龙づ霸主 提交于 2019-11-27 12:05:30
I see that sometimes NSImage size is not real size (with some pictures) and CIImage size is always real. I was testing with this image . This is source code which I wrote for testing: NSImage *_imageNSImage = [[NSImage alloc]initWithContentsOfFile:@"<path to image>"]; NSSize _dimensions = [_imageNSImage size]; [_imageNSImage release]; NSLog(@"Width from CIImage: %f",_dimensions.width); NSLog(@"Height from CIImage: %f",_dimensions.height); NSURL *_myURL = [NSURL fileURLWithPath:@"<path to image>"]; CIImage *_imageCIImage = [CIImage imageWithContentsOfURL:_myURL]; NSRect _rectFromCIImage = [

NSImage to NSData as PNG Swift

笑着哭i 提交于 2019-11-27 07:36:10
问题 I am writing a Mac app based on an iOS app. The code below converts a UIImage to NSData to upload to Parse.com. I would like to do the same for Mac but I do not seem to be able to convert it to NSData. What should I be doing? Thanks var image = UIImage(named: "SmudgeInc") let imageData = UIImagePNGRepresentation(image) let imageFile = PFFile(name:"image.png", data:imageData) 回答1: You can use the NSImage property TIFFRepresentation to convert your NSImage to NSData : let imageData = yourImage

How to save PNG file from NSImage (retina issues)

戏子无情 提交于 2019-11-27 00:45:56
I'm doing some operations on images and after I'm done, I want to save the image as PNG on disk. I'm doing the following: + (void)saveImage:(NSImage *)image atPath:(NSString *)path { [image lockFocus] ; NSBitmapImageRep *imageRepresentation = [[NSBitmapImageRep alloc] initWithFocusedViewRect:NSMakeRect(0.0, 0.0, image.size.width, image.size.height)] ; [image unlockFocus] ; NSData *data = [imageRepresentation representationUsingType:NSPNGFileType properties:nil]; [data writeToFile:path atomically:YES]; } This code is working, but the problem is with retina mac, if I print the NSBitmapImageRep

How do I take a “screenshot” of an NSView?

雨燕双飞 提交于 2019-11-26 19:55:06
问题 I need to take the contents of an NSView and put them in an NSImage , for an experimental project. Is this possible? I did some Googling, tried two methods that I found - but they didn't really work. Any suggestions? 回答1: [[NSImage alloc] initWithData:[view dataWithPDFInsideRect:[view bounds]]]; 回答2: From WWDC 2012 Session 245 (translated to Swift): let viewToCapture = self.window!.contentView! let rep = viewToCapture.bitmapImageRepForCachingDisplayInRect(viewToCapture.bounds)! viewToCapture

How to resize NSImage?

笑着哭i 提交于 2019-11-26 18:09:22
问题 I have an NSBitmapImageRep which is W x H size. I create NSImage and call addRepresentation: . Then I need to resize the NSImage . I tried setSize method but it doesn't work. What should I do? 回答1: Edit: Since this answer is still the accepted answer, but was written without Retina screens in mind, I will straight up link to a better solution further down the thread: Objective-C Swift 4 Because the method of Paresh is totally correct but deprecated since 10.8 I'll post the working 10.8 code

How to save a NSImage as a new file

醉酒当歌 提交于 2019-11-26 17:02:52
How can I save a NSImage as a new file (png, jpg, ...) in a certain directory? garph0 Do something like this: NSBitmapImageRep *imgRep = [[image representations] objectAtIndex: 0]; NSData *data = [imgRep representationUsingType: NSPNGFileType properties: nil]; [data writeToFile: @"/path/to/file.png" atomically: NO]; You could add a category to NSImage like this @interface NSImage(saveAsJpegWithName) - (void) saveAsJpegWithName:(NSString*) fileName; @end @implementation NSImage(saveAsJpegWithName) - (void) saveAsJpegWithName:(NSString*) fileName { // Cache the reduced image NSData *imageData =

NSImage size not real size with some pictures?

北战南征 提交于 2019-11-26 15:54:03
问题 I see that sometimes NSImage size is not real size (with some pictures) and CIImage size is always real. I was testing with this image. This is source code which I wrote for testing: NSImage *_imageNSImage = [[NSImage alloc]initWithContentsOfFile:@"<path to image>"]; NSSize _dimensions = [_imageNSImage size]; [_imageNSImage release]; NSLog(@"Width from CIImage: %f",_dimensions.width); NSLog(@"Height from CIImage: %f",_dimensions.height); NSURL *_myURL = [NSURL fileURLWithPath:@"<path to image

How to save PNG file from NSImage (retina issues)

江枫思渺然 提交于 2019-11-26 09:26:07
问题 I\'m doing some operations on images and after I\'m done, I want to save the image as PNG on disk. I\'m doing the following: + (void)saveImage:(NSImage *)image atPath:(NSString *)path { [image lockFocus] ; NSBitmapImageRep *imageRepresentation = [[NSBitmapImageRep alloc] initWithFocusedViewRect:NSMakeRect(0.0, 0.0, image.size.width, image.size.height)] ; [image unlockFocus] ; NSData *data = [imageRepresentation representationUsingType:NSPNGFileType properties:nil]; [data writeToFile:path