How to create multi-resolution (Retina-ready) NSImage programmatically at runtime? - objective-c

I have two button images, say "myButton.png" and "myButton#2x.png" in my app's bundle resources. At run time, I load the image using [NSImage imageNamed:#"myButton"] and need to split it into three slices and create three NSImages to use in NSDrawThreePartImage() function.
The problem is that each slice NSImage needs to be multi-resolution, so that the system could pick the right resolution dynamically, as it would automatically do with the original whole image.
How do you create the automatic multi-resolution NSImage programmatically? Thanks!

I think the appropriate solution is to store the image parts in different images and don’t try to code a workaround that splits one image into three parts. This way you can use NSDrawThreePartImage() directly and in the most efficient manner.

Related

SDL2: Share renderer between multiple windows

I have a set of images, and I need show it on different displays. So I create two windows and two renderers. But some image may be show on several displays. And if the texture was created using rendererOne, and shown with rendererTwo, we have a program crash.
If I create texture in runtime each time, when I need show - I have falling of FPS.
How it is better to solve this problem? Can I share renderer between windows (on different displays)? Or can I share texture between different renderers?
p.s. I can mark image's name like "Image1.one.two.png" or "Image2.one.png" and so on, and create two copies of Image1 and one copy of Image2, but it very difficult way, and require many RAM.
p.p.s. I don't use OpenGL directly.
I have solved this problem by using a lazy initialization of texture. I store SDL_Surface, and when I need show some Texture then I check it:
if (m_texture == nullptr || !m_texture->CompatibleWithRenderer(renderer))
{
m_texture = new Texture(renderer, m_surface);
}

Do repeated images increase the loading time of a website?

I'm trying to optimize the loading time of my website.
Does requesting the same image in different places of the code increase load time?
For example, I'm using an image as background of a button and I'm loading the same image in different places of the website.
Is this a problem or since the file is the same in all the buttons the browser only loads it once?
If it's a problem, how can I optimize it? Thanks a lot!
no, repeating the same image multiple times should load it only once. You could even go further and actually use css sprites to have multiple images in one (one image which is a grid of images as a background within a smaller containing div and move it around with background position property)

Ideal image size and format

I'm building an image gallery type of application and i want to know what is the best image size and format (.png or .jpg) to be stored in the app.
I also want to know what is the best and most efficient way to store and load images natively.
PNGs are optimized for the OS when they are added to your app bundle. To improve performance of your app you want to:
Make sure the images have no alpha channel in them, otherwise the OS will try and blend them.
Resize your images to be the size they will end up in the UI. This will reduce the read bandwidth for the drawing, saving you memory and boosting performance.
Images in the app bundle can be read into UIImageViews:
UIImageView *newImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"myimage.png"]];
This will cache the image for you, which is good for lots of small images re-used a lot. For larger infrequently used images you want to use +imageFromContentsOfFile: instead of +imageNamed:
As far as I know iOS and xCode optimizes PNG formats and performs better with them.
You may want to check out this :
http://bjango.com/articles/pngcompression/

Spritesheet: tool to get coordinates

I have a one spritesheet image with all sprites, I downloaded this image from Interent. My question: Is there a tool that help me to get the coordinates of each sprite? would I have to search coordinates manually ?
I've been reading several tutorials like:http://www.raywenderlich.com/1271/how-to-use-animations-and-sprite-sheets-in-cocos2d and http://indiedevstories.com/2011/04/10/using-sprite-sheets-in-cocos2d-and-tiled-part-1/, but all them use multiple images to create one spritesheet and generate automatically the .plist, but this is not my case.
In my case I have only one .png and no .plist
Help me please!!!
Apologies for the self-promotion but I have created a tool for working with single spritesheet images and outputting the coordinates. It even has automatic sprite selection :)
http://www.darkfunction.com/editor
You can crop the images using some editor (photoshop for example) and then use the exiting tool to generate the texture and the plist. I think it will be the fastest and the easiest way because in case you would want to generate only plist you will do the same operation - selecting the rectangle.
Use preview to crop out all the images separately. Then use zwoptex or texture packer to combine them all and generate the spritesheet with .png and .plist
Other than that it is difficult to figure out what is the rect of each image. If you are a good programmer you can write a tool to analyze the png and extract out the information which can be used to identify the rectangles, but my advice is to do it manually.
Divide your image using an image editor. Then add it to a spritesheet generator (like Sprite Master). Then you are free to make your output as you want by just changing the parameters.
Sprite Master will have a feature like parsing prepared spritesheet to individual images in following versions.

iPhone Objective-C image manipulation

I am looking for a way to, in Objective-C, create a PNG from several smaller PNGs based on how the user sets things up. Is this possible using existing Apple classes, or do I need to use a 3rd party library? If 3rd party code is needed, can anyone recommend a good library? The simpler the better - simple filters (such as darkening/lightening the image) would be nice but not required.
Here is some pseudo-code, to give you a better idea of what I am looking for:
image = [myImageLibrary imageWithHeight:1024 width:768];
[image addImage:#"background.png" atX:0 andY:0 withRotation:0];
[image addImage:#"image2.png" atX:100 andY:200 withRotation:90];
[image saveAtLocation:#"output.png"];
At output.png we see image2.png placed on top of background.png and rotated 90 degrees
P.S. - I am sorry if this seems to be a duplicate of another question, I just have not found an answer that works for what I am trying to do.
Have you read the "Creating and Drawing Images" section of the Drawing and Printing Guide for iOS and the UIImage Class Reference docs?
What you're after is perfectly possible - with a well built class you could pretty much use that pseudo code as-is.
As a starter for ten, you could:
Create your own graphics context via UIGraphicsBeginImageContext.
Draw into that via the drawAtPoint: method of the UIImage class
Save the resultant image data out via UIGraphicsGetImageFromCurrentImageContext.
In terms of steps 1 and 3, see the UIKit Function Reference for more info. Additionally, the imageWithCGImage:scale:orientation: method of the UIImage class may prove useful for performing transformations, etc. as a part of step 2.
You'll want to look at CGContextDrawImage to draw your images, using a custom bitmap context, and then save it out using UIGraphicsGetImageFromCurrentImageContext(). The rotation can be done by applying CGAffineTransforms to your CGContext.
More information on Core Graphics here:
http://developer.apple.com/library/mac/#documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/Introduction/Introduction.html