I have to make a prototype application where I superimpose a small image over the file icons of a given folder.
Let's say I have a folder /MyDocuments/
and there are three files /MyDocuments/Doc1.rtf /MyDocuments/Doc1.pdf and /MyDocuments/Doc1.jpg
and I have an image myicon.png, now I have to superimpose this image myicon.png over the file icons of all the three files present in /MyDocuments/
I understand that I can use the methods in NSWorkspace sharedWorkspace to get and set the file icons for these files, but I have no idea how to use the image myicon.png and superimpose it over the existing icons of these files.
If anyone has seen the Dropbox application (dropbox.com), then it is similar to the way you see changed icons in your dropbox folder
I assume it would be done using NSImage but I have no idea how to do it.
Note: the image myicon.png will only occupy the top left part of the original icon of these files i.e. the image should not completely overlap with the existing icons but only the 1/4th portion on the top left should be occupied.
Lock focus on the file icon, then draw the badge icon, then unlock focus. You may want to do this to a copy of the file icon, and hang on to the unbadged original.
If the badge is one of the standard badges that come with Mac OS X, don't copy the badge into your app—it'll look outdated if Apple ever changes it. The standard badges are named in IconsCore.h; you can wrap any of those types in a string using the NSFileTypeForHFSTypeCode function, then pass that string to NSWorkspace's iconForFileType: to get the standard badge as an image, from which point you can do the above.
As a supplement to Peter Hosey's answer, here is some slightly modified example code from:
http://cocoadev.com/forums/comments.php?DiscussionID=221
NSImage *origImage = [sourceImage copy]; // Copy to avoid modifying the original.
NSSize previewSize = NSMakeSize([origImage size].width / 4.0, [origImage size].height / 4.0);
NSImage *previewImage = [[NSImage alloc] initWithSize:previewSize];
[previewImage lockFocus];
[origImage drawInRect:NSMakeRect(0, 0, previewSize.width, previewSize.height)
fromRect:NSZeroRect // Draws full image.
operation:NSCompositeSourceOver
fraction:1.0];
[previewImage unlockFocus];
Related
I have an NSTextAttachment which contains an NSImage. What I would like to do is scale this image so that it's smaller in dimensions but will keeping the same number of pixels. Then moving it to the general pasteboard. Once it's pasted (into whatever app) I would like it to paste with the same dimensions I just set. The problem I am having is that pasting it in any other app always puts it at is original dimensions. I have tried the following things and none seem to fully work
First is simply resizing the image and then copying. This retains the proper dimensions (let's say 100 x 100 pt) but the image is very blurry and the resolution is 72 dpi.
//variable im is an NSImage object that is present
//variable newsize is an NSSize which is calculated to give the image proper resolution
im.lockFocus()
im.size = newsize
im.unlockFocus()
The second thing I have tried is use the techniques listed on this page. None of which do anything once I paste the TextAttachment to another app (such as Pages for example). Subclassing NSTextAttachment has no effect. Also changing the bounds of the attachment seems to have no effect once it's pasted.
The last thing I tried is after the iterating through the NSAttributedString which holds the attachments, and setting each one's bounds. Still no effect when pasting into other apps.
So the ultimate question: is there a way to have a scaled NSImage inside and NSTextAttachment copy to the clipboard?
func copy() {
let pboard = NSPasteboard.general()
pboard.clearContents()
let rtf = NSMutableAttributedString()
//self.images is an array that contains a bunch of images
//note that it is required that I use a RTF to copy/paste my images
//because there is text mixed with the images
//for simplicity I have removed the text from this code
//removing it from my app and doing the same code has no effect
for im in self.images {
let a = NSTextAttachment() //subclassing NSTextAttachment has no effect on pasted image size
a.image = im
a.bounds = NSMakeRect(0,0,100,100) //has no effect on pasted image size
//if I resize the image prior to copying (using the code above)
//the pasted image has correct dimensions but is very blurry.
let str = NSAttributedString(attachment: a)
rtf.append(str)
}
pboard.writeObjects([rtf])
}
In my app I use initWithContentsOfURL to load various types of image (JPEG, TIFF, PNG, GIF, etc) into an image, and then into an OpenGL texture.
The only type that loads an image with an alpha channel is png. (in the list above, only PNG and TIFF can contain alpha data.) If I try to load a .tiff image, it gets loaded without an alpha channel (the image's image rep reports alpha=NO, and it reports bitsPerPixel of 24.
I can edit an image with alpha in PS, save it as a PNG and a TFF, and the PNG loads in my program with alpha but the TIFF does not. Further, I can open the TIFF image in PS and confirm that it does have alpha data.
What am I missing here? Why are my TIFF images not loading with an alpha channel? And is there another appkit call I can make that WILL load my TIFF without dropping the alpha channel on the floor?
EDIT:
Since posting this question I've found that some 4-channel TIFFs load with alpha data and some do not. I have not yet figured out what workflow results in the different results.
This file loads with an alpha channel in Photoshop, but not if you load it in Cocoa using -[[NSImage alloc] initWithContentsOfURL]:
Image "Red Julia Seahorse crop"
A similar image that also has an alpha channel DOES load with alpha using the above Cocoa call:
Image "Transparent Seahorses"
I just tried (OSX 10.9.4) and the image loaded complete with graduated transparency. The code I used is trivial:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application
NSURL *imageURL = [NSURL URLWithString: #"file:/Users/john/Desktop/test.tiff"];
NSImage *image = [[NSImage alloc] initWithContentsOfURL: imageURL];
self.imageView.image = image;
}
I created a TIFF using layers and without layers (two tests). Both worked. I tried a couple of different NSImageView backgrounds to verify the graduated transparency was genuine. (If you choose different Image View border styles in IB the background colour changes too).
I used Photoshop 12 (CS5) to create the images, and manually checked the 'Save Transparency' checkbox in the TIFF Options dialog when saving.
Hopefully something here helps you home in on your issue. From my tests it all works as expected.
The issue is definitely with how the images were saved, most likely the TIFF options used.
Using your two images, the "Red Julia Seahorse Crop" image did not display with transparency, but the "Transparent Seahorses" displayed correctly with transparency.
I opened the "Red Julia Seahorse Crop" in Photoshop and re-saved the image (unchanged), but made sure the "Save Transparency" check box was selected in the "TIFF Options" dialog box. Once saved, that image now showed transparency correctly in the application.
I saw one of the Apple Videos mention that you can colour images via the code. All my searches on how to do this came up blank.
If I have a black vector image (pdf) saved inside Images.xcassets, how can I colour that image at run time?
Ideally it would be something simple like [UIImage setVectorColor:UIColorBlue] but I'm sure there could be more to it!
You have to set all vectors image as Template Image on Render options in the xassets. (http://i.stack.imgur.com/oTuDC.png)
After, you can set the color in the uiimageview which contain your image with method :
[imageView setTintColor:[UIColor redColor]];
I want to create an NSImage of an NSScrollView object, so I can use the flat graphic for animation purposes.
When I render my scrollview object into a graphic and add it back to my window, it works but looks really bad like it's been scaled to 99% or something. I want the image to not be scaled and 100% pixel accurate. (Note: the image isn't scaled, it's the same size, it just looks like it's been poorly rescaled - the text looks rough and poor compared to the view onscreen in the scrollview)
My code:
(scrollView is my NSScrollView object)
NSData *pdf = [scrollView dataWithPDFInsideRect:[scrollView bounds]];
NSImage *image = [[NSImage alloc] initWithData:pdf];
NSImageView *imageView = [[NSImageView alloc] initWithFrame:[scrollView bounds]];
[imageView setImage: image];
[mainGUIPanel addSubview: imageView];
I've tried a heap of things, messed with pixel sizes, bounds, used IB to create the destination NSView and put the image inside that but just cannot get the image to not look bad. Any ideas?
Edit:
I tried writing the pdf data to a pdf file and viewed it, and it looked ok. So the bitmap image is being captured ok, it's just on the display that it looks like it's being scaled somewhat.
Edit2:
Also tried getting the bitmap like this:
NSBitmapImageRep *bitmap = [scrollView bitmapImageRepForCachingDisplayInRect:[scrollView bounds]];
[scrollView cacheDisplayInRect:[scrollView bounds] toBitmapImageRep:bitmap];
NSImage * image = [[NSImage alloc] initWithSize:[bitmap size]];
[image addRepresentation: bitmap];
Same results - the bitmap looks exactly the same, bad and scaled when displayed.
This leads me to believe that capturing the bitmap data either way works fine, it's creating the view and rendering the image that is doing the scaling. How can I make sure that the view and image are shown at the correct size and scaling?
Edit3:
Ok, I started a new blank project and set this up, and it works perfectly - the new imageview is identical to the grabbed bitmap. So I suspect my issue is stemming from some rendering/compositing issue when drawing the bitmap to the view. Investigating further...
It turns out the issue stems from the scrollView that I am rendering from. This has a transparent background (Draw Background is off in IB) and the text is the scrollView looks good. If I turn "Draw Background ON", with a transparent background color, the text is rendered badly, exactly as it is when I capture the image programatically.
So, in my app, even though Draw Background is off, the scrollView image is captured as though Draw Background is on. So I need to understand why the text is rendered badly when Draw Background is on and set to transparent, and hopefully this will lead me towards a solution.
Also tried creating an NSClipview with background drawing turned off and putting the bitmap view into that, but it sill renders the same. I can't find a way to render the transparent image to the screen without horrible artifacting.
Ok, I've found a solution. Instead of getting a grab of the transparent background scrollview object itself, I'm instead getting a grab of the parent view (essentially the window background), and restricting the bounds to the size of the scrollview object.
This captures both the background, and the contents of the scrollview, and displays correctly without any issues of transparency.
I have an app that currently has this line:
[myView setWantsLayer:YES];
In order to draw a GUI element via NSBezierPath. This line is required, otherwise when the user types in an adjacent (and overlapping) NSTextField, the contents of myView shudders.
I discovered that calling CoreAnimation loads the OpenGL framework, but does not unload it. See this question.
I think I can get around this by drawing the NSBezierPath to NSImage and then to display the NSImage in lieu of the NSBezierPath, but I haven't found a single source that shows me how to go about this.
Edit:
I should note that I want to save this BEFORE The NSBezierPath is displayed - so solutions that draw an existing view to an NSImage are not useful.
Question:
Can someone point me in the right direction for converting NSBezierPath to an NSImage?
You can draw anything directly into an NSImage, and you can create a blank NSImage. So, create an image whose size is the size of the bounds of the path, translate the path so that it's at the origin of the image, and then lock focus on the image, draw the path, and unlock focus.