how to draw with UIGraphicsBeginImageContext() - ios

Im trying to generate an image, and have found UIGraphicsBeginImageContext() in the Apple docs which looks perfect. Ive been looking at some Quartz tutorials and in each one they use a custom view to do their drawing which doesn't seem necessary in this case, but i dont know enough to be sure. Whats the best way to do my drawing using UIGraphicsBeginImageContext()?

Well, you probably want to use UIGraphicsBeginImageContextWithOptions() and scale=0.0 to get resolution independence, but yes, once you call the function the frameworks will have set up a normal graphics context you can use as in the tutorials. You can get at it with UIGraphicsGetCurrentContext().
When you have finished drawing you will likely want to use UIGraphicsGetImageFromCurrentImageContext()to actually capture what you have drawn.
And don't forget to call UIGraphicsEndImageContext() when finished.

Related

Whats the fastest way to a draw grid of color on IOS?

Im trying to continually update a grid of colors on my iPhone screen ( testing with 50x50, but would like to scale up later ) I have done some research but can't seem to find an agreed upon solution. I've tested CAShapes and UIBezier paths and colored UIViews, but everything is slow. Is there another option besides diving into OpenGL or Metal? Doesn't need to be crazy fast, just faster than the before-mentioned options. Thanks, I'm working in Objective-C
If you don’t want to dive into Metal then what I found much quicker for an app I wrote years ago was to put my data into a byte array and then use that array to render a bitmap image.
I don’t have all the details now. It used something like an “image provider” and various other parts. But it was much quicker than any other method I tried.
I was able to draw over 5000 “pixels” per frame using it so it should be good For you now.
Then you can either draw it into a view in drawRect or put it into a UIImageView.

When to switch from drawRect: to OpenGL/Metal?

I've been building a 2D game and I've been drawing the playing area inside of a UIViewsubclass where I override drawRect: and draw the game with a lot of UIBezierPath* objects. I'm not very experienced on iOS and I've been wondering if this is the right way to do it.
So I guess my question is How much is enough? When should I stop using UIBezierPathand start using OpenGL or Metal? Can I use these to draw inside an UIViewor they take total control of the screen?
There is no answer on when you should stop using UIBezierPath. You need to ask yourself in the beginning on what tool will you need to use to achieve what you need for your application to work the way you want it to. The core graphics procedure which you use is very simple comparing to openGL or such but the performance is not at its best and mostly you are very limited on what you can even achieve in drawing. In general you should use as easy procedure as possible as long as it works out for you.
OpenGL and Metal are bond to the view and do not take control of your whole window (the screen in iOS). Also you can still add subviews to those views without breaking any drawing or functionality so for instance even a full screen view openGL application can have a simple UIButton to for instance pause the game or make your main character take that big sword and slay the dragon saving the princess.

Stretching a UIImage across the length of a UIBezierPath

What I basically need to achieve is a Fruit Ninja - style "slash" effect, where the "slash" trails the user's touch and follows the shape of the user's gesture, and is thinner the longer the distance the user has swiped.
The simplest way to achieve this seemed to be to collect all the points the user passes through in a UIBezierPath, and "stretch" an image through the length of the BezierPath. This would achieve the kind of "trailing" effect I was looking for and also ensure that the line is thinner if the distance travelled is longer.
However I can't seem to find a way to actually implement this. Is this even possible?
Alternatives? Thanks.
P.S: This is for a low-medium priority section of a regular app and not a game, so I would like to avoid having go down to OpenGL and spend a lot of time to achieve this (with completely custom drawing, etc). Something at the SDK level would be preferred, and if that's not possible at all, we'll just figure out a different UI.
Thanks!
For pretty easy-to-use stretching teqhniques of images/views you could look into
https://github.com/hfossli/AGGeometryKit/
I recommend trying to draw using CoreGraphics. See this link
http://www.effectiveui.com/blog/2011/12/02/how-to-build-a-simple-painting-app-for-ios/
Okay. Maybe you can use this.
https://github.com/hfossli/AGDraw
Just something I wrote a while ago. Hit clear and try to draw something (clear will toggle between two types of strokes). You'll see the width of the penstroke will increase with the velocity you use.. I guess that fits your need. If you fix some bugs, please make a pull request. You are free to use the code, but I will add a MIT license later.

Apply GPUImage filter to a UIView

I've a problem. I need to apply a filter like Pixelate or Blur to an entire UIView.
Like the eBay iPad app.
I thought to use GPUImage but I don't know how to do it.
There is a way to apply a filter to a GPUImageView directly without pass a UIImage?
The primary problem is that making a screenshot of a large UIView on an iPad 3rd is to expensive (2 seconds for the UIWindow grab). So the perfect solution is to apply filter directly to the views, just like eBay app, but.. how?
Thanks to all!
To pull a view into GPUImage, you can use a GPUImageUIElement source, which takes a UIView or CALayer as input. There's an example of this in the FilterShowcase sample application.
This does rely on the -renderInContext: method of an underlying CALayer, which can be expensive for redrawing the view. However, if the view is static, you just need to use this update once and the resulting image will be cached on the GPU as a texture. Filter actions applied to it after that point will be very fast.
You might be able to achieve the look you are after by applying CIFilters to your views layer.filters property. Check the docs for more info:
https://developer.apple.com/library/mac/#documentation/graphicsimaging/reference/CALayer_class/Introduction/Introduction.html
Maybe this is something for you? Haven't tried it but read about it in a post once:
StackBlur
Ow sorry, I read your post again and this extension is about blurring an UIImage, and you said that this was something you didn't want...
Well I'll leave it here anyways if people go googling for blurring an image..
Sorry :(

How can i add eraser cocos2d?

This is my cocos2d screen. And i wanna add eraser.(not clean just erase the lines like an eraser).The background is image. So it shouldn't be erased.How can i do that?
Edit: i used this code. https://github.com/krzysztofzablocki/smooth-drawing
Yatanadam, I think your original question is quite broad and to be able to solve all your problem it would require someone to understand all the code of the sample you want to use. You would find an answer faster if you asked for specific pieces of the code or provided more information about the sample.
I did a quick check and in the class LineDrawer.m there is a method called draw, that first calculates the points needed for the curves you want and then calls drawLines to do the actual job of drawing. You could try to calculate which points need to be eliminated and then delete them from the array points. Every time the user touches the screen to erase you would need to delete points from the array to make it work.
Still, what you are asking requires a deeper understanding of the sample

Resources