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]];
Related
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 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.
In have an NSImage is a template image (that is, [NSImage isTemplate] returns YES).
When I use it inside an NSImageView, it is drawn correctly as a template image.
However, if I draw it manually using drawInRect:fromRect:operation:fraction:, it is drawn as a flat black image.
How can I draw an NSImage manually, and still get the 'template' effect?
The special drawing of template images is part of CoreUI and not public.
You can imitate the effect with Quartz though. Details and code can be found in this answer here on SO:
https://stackoverflow.com/a/7138497/100848
In my program, I need to add some images on the GUI. However, the background color of the image is not the same as the background color of the view.
I know that there is a property used to change the image background. However, when I change the feature of this item, there is nothing happen. Do I miss some settings before adding the image on the project such that I cannot change the background color of the image? Thank you so much for your emergent help!
Change of ImageView's background is possible, but not of Image's.
You can do it by specifying the desired color of ImageView.
[imageViewName setBackgroundColor:[UIColor redColor]];
Or you can specify the RGB values.
[imageViewName setBackgroundColor:[UIColor colorWithRed:0.5 green:0.0 blue:1 alpha:1.0]];
You can't change the background of the image. But you can change the background color of imageview that contains the image.
I have
UIView *topPart = [[UIView alloc] initWithFrame:CGRectMake(9, 0, 302, 318)];
topPart.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"pattern.png"]];
[someScroller addSubview:topPart];
[topPart release];
and I can see the pattern fine when I use a 3gs or the iphone simulator, but when I tested on an actual iPhone 4, I see the pattern images but there are black lines separating each pattern so it looks like a grid. I checked the pattern image and there is no black border or anything .
There was someone with a different problem but it may be the same solution
colorWithPatternImage with iPhone 4 Retina Display (image#2x.png)
but they suggested using the -(void)drawRect: method, only problem is i have no idea how to do that.
Thanks
I was experiencing the same issue and found it was due to my png being greyscale (not RGB)
To check if this is the case, just select the image in Xcode, show the Utilities sidebar (the one that appears from the right) and look at the Color Space.
To fix this you can use your favourite image editor, but for photoshop do this:
Open the greyscale image
Select all and copy
Create a new document (but this time make sure the colour space is RGB)
Paste
Hope it helps :)
I figured it out by combining the answers from these 2 posts
https://devforums.apple.com/message/259150#259150
How can I use CGContextDrawTiledImage to tile an image?
UIImage *thePattern= [self GetFineTiles:[UIImage imageNamed:#"Feedbackpattern.png"]];
theView.backgroundColor = [UIColor colorWithPatternImage:thePattern];
-(UIImage*)GetFineTiles:(UIImage*)badImage{
UIGraphicsBeginImageContext(badImage.size);
CGContextRef imageContext = UIGraphicsGetCurrentContext();
CGContextDrawTiledImage(imageContext, (CGRect){CGPointZero,badImage.size}, badImage.CGImage);
UIImage *goodPattern = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return goodPattern;
}
Hope this helps others