How to recolor an image on specific area? - ios

I'm trying to create an iOS recoloring app (this is my reference), and i need to know how recolor some portion of the image when user taps on a given area. All the loaded pictures will be black/white initially.
Is there any prebuilt library? Or which graphics framework should i use?
Any help will be appreciated.

If what you are looking for is adding/replacing the colour within a certain shape and edges are really important (as in the example) then you should be looking into vectorised drawing.
What this means is every shape in your image would have an actual object representation in your code, and you could easily interact with that object to do whatever you want (i.e. tap gestures to change colour, zoom etc.).
This however, means that you can't simply use .jpeg images, and you need to use images in vector format, such as .svg or CorelDraw.
As a reference, check out SVGKit, which is an excellent library for working with SVG images.

Related

How to create UIButton with custom shape in Swift?

I need to create button like this
and the change the background programmatically like this
and like this
I can not use images for different states of a button because each time I have different text on it.
What to start from ? I tried to understand CoreGraphics and CoreAnimation but there is too small amount of examples and tutorials so my attempts didn't give me any success.
You can, and should, use an image for this. UIKit has a method resizableImageWithCapInsets that creates resizable images. You feed it a minimum sized image and the system stretches it to fit the desired size. It looks like your image is fixed in height, which is good since you can't do smooth gradients with this technique.
UIButtons are composed of a background image and title text, so you can use an image for the background shapes (setBackgroundImage(_:forState:)), and then change the text using setTitle(_:forState:).
However, you can still use Core Graphics for this, and there are benefits to doing so, such as the fact that it reduces the number of rendered assets in your app bundle. For this, probably the best approach is to create a CAShapeLayer with a path constructed from a UIBezierPath, and then render it into a graphics context. From this context, you can pull out a UIImage instance, and treat it just the same as an image loaded from a JPEG or PNG asset (that is, set it as the buttons background image using setBackgroundImage(_:forState:)).

Custom shaped buttons objective-c

I'm building app for kids with lots of interactive image objects (click on pic & get result)
For example: I have cat on my screen and with click on it I need to produce sound.
After having read lots of info I got the idea that it's almost impossible to transform certain parts of image to clickable objects.
While there are lots of games on the app store which contains lots of custom shaped clickable objects. How did they manage to do this?
What I need read to get the answer? Thank you in advance.
UPDATE: I can have my clipart images as vector-graphic, e.g. .svg file. Will it make the situation simpler?
Implement each image as a UIButton and set the background of each button as the image desired. (setBackgroundImage)
Links:
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIButton_Class/
Using images is one option, but if you want taps within a certain non rectangular shape, check out OBShapedButton
If you really want super accurate touches, you could construct a bezierpath in the shape of the image then use
[UIBezierPath containtsPoint:]
https://developer.apple.com/library/ios/documentation/uikit/reference/UIBezierPath_class/Reference/Reference.html#//apple_ref/occ/instm/UIBezierPath/containsPoint%3a
You could also possibly get the pixel colour underneath the touch, but that wouldn't give much room for error if the user tapped near the edge:
https://stackoverflow.com/a/7101544/78496

Adding border to edges of opaque area of UIImage with a filter

Hello: Currently in my project, I'm using OBShapedButton to process touches on a lot of objects that overlap (it's a map with each territory its own separate object). Basically, this library prevents a touch from being processed on a transparent point on the given view.
I'm attempting to add a border effect to just the edges of the opaque part of the UIImage (and adding a semi-transparent overlay above that). Something to the effect of this:
Which can be simplified to this (example of one image):
I am currently using MGImageUtilities to color in the opaque parts of territories using this line:
[territory setImage:[[territory image] imageTintedWithColor:tint]];
The problem is that I'm not sure how to just color the borders (which can be any shape). I've looked at this link already, but haven't been able to come up with anything.
Thanks in advance for the help!
Terribly hacky, but use MGImageUtilities' UIImage+ProportionalFill with scale resizing to create a slightly larger image, UIImage+Tint to red, and stack below.
The library you are using doesn't actually specify a shape layer. It uses alpha values from the PNGs that you give it.
Could you use a different 'highlighted' or 'selected' PNG that adds the border effect you are looking for?
Otherwise, it you will have to generate a UIBezierPath from your PNG image, which sounds like a very computationally intensive operation. At that point, I might question whether this library meets your needs.

How can I change hue of an UIImage programmatically only in few parts?

How can I change hue of an UIImage programmatically only in few parts? I have followed this link
How to programmatically change the hue of UIImage?
and used the same code in my application. It's working fine but the complete image hue is getting changed. According to my requirement I want to change only the tree color in the above snap. How can I do that?
This is a specific case of a more general problem of using masking. I assume you have some way of knowing what pixels are in the "tree" part, and which ones are not. (If not, that's a whole other question/problem).
If so, first draw the original to the result context, then create a mask (see here: http://mobiledevelopertips.com/cocoa/how-to-mask-an-image.html), and draw the changed-hue version with the mask representing the tree active.
I recommend you take a look at the CoreImage API and the CIColorCube or CIColorMap filter in particular. Now how to define the color cube or color map is where the real magic lies. You'll need to transform tree tones (browns, etc), though this will obviously transform all browns, not just your tree.

overlaying images when displaying in OpenCV

I have two images that I want to display on top of each other. one image a single channel image and the second image is a RGB image but with most of the area being transparent.
How these two images are generated in different functions. I know to just display these on top of each other, i can use the same window name when calling cvShowImage() but this doesn't work when they are drawn from different functions. When trying this, I used cvCvtcolor() to convert he binary image from single channel to RGB and then displaying the second image from another function. But this didn't work. Both images are same dimension, depth and number of channels (after conversion).
I want to avoid passing in one image into the second function and then draw them. So I'm looking for a quick dirty trick to display these two images overlapped.
Thank you
EDIT:
I don't think that's possible. You'll have to create a new image or modify an existing one. Here's an article that shows how to do this: Transparent image overlays in OpenCV
There is no way to "overlay" images. cvShowImage() displays a single image from memory. You'll need to blend/combine them together. There are several ways to do this.
You can copy one into 1 or 2 channels of the other, you can use logical operations like AND, OR or XOR, you can use arithmetic operations like Add, Multiply and MultiplyScale (these operations will saturate values larger than 255). All these can also be done with an optional mask image like your blob image.
Naturally, you may want to do this into a third buffer so as not to overwrite your originals.
Apparently now it can be done using OpenCV 2.1 version
http://opencv.willowgarage.com/documentation/cpp/highgui_qt_new_functions.html#cv-displayoverlay

Resources