Draw text and add to a UIImage iOS 5/6 - ios

I have a textbox, where i want the written text to be added to a UIImage.
How can i draw NSString to a UIImage?
I´ve searched, and found lots of examples, but non of them works. Xcode just gives me lots of errors.
Simply put, i want to draw a NSString to a UIimage. The UIImage should be the same size as a predefined UIImageView, and be placed in center.
Any ideas?

i think solution is here..i have used this method in one of my application
here lbltext is the object of my label. this method creates new image with given text & return the object of new image with text.
-(UIImage *) drawText:(NSString*) text inImage:(UIImage*)image atPoint:(CGPoint)point
{
UIFont *font = lblText.font;
UIGraphicsBeginImageContext(iv.frame.size);
[image drawInRect:CGRectMake(0,0,iv.frame.size.width,iv.frame.size.height)];
CGRect rect = CGRectMake(point.x,point.y,lblText.frame.size.width, lblText.frame.size.height);
[lblText.textColor set];
[text drawInRect:CGRectIntegral(rect) withFont:font];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
in your below comment you have passed iv.center as a point.. try manualy like this
CGPoint point;
point.x=30; // dynamicaly just for example lblText.frame.origin.x;
point.y=40; //lblText.frame.origin.y;
you have to call above method like this...
UIImage *img = [self drawText:#"Test String" inImage:originalImage atPoint:point];
here,img is another instace of UIImage for storing new image with text...after calling this method use img imager for your further use..
for example...
[newimageviewObj setImage:img];
i hope this will help you

UIImage is not a subview of UIView, so you cant add a subview to it. Also NSString is not a subview of UIView. If you want to show things on the screen, they should inherit from UIView.
So try this:
Create a UIImageView - set its image property to be your UIImage instance.
Create a UILabel - set its text property to your NSString.
Add the UILabel as a subview of your UIImageView.
and finally add your UIImageView to your current view controllers view.

Emm, here is some thoughts.
I think that one simple way is like this :
Put aUIImageView on aView;
Add aUITextView on aView;
Get ScreenShot from aView;
This may works fine.
Also, this may come with a problem that screenshot may be not clear.
Then, After step1 and step2, we may get new image by UIGraphics.
(With here, we already know position where text should be on image and this should be ok.)
PS: Some image may not have some attribution like CGImage and CGImage is needed for this. So, transform it.

Related

How to draw/paint UIIMage on view as paint without UIImageView in iOS

I'm developing painting app in this app I'm using ACEDrawing tool to draw over the view. But I've to cut particular portion of the painting and to paste at anywhere on the view.
I can copy the paintings of the view as an image. my code is here.
In .h file
#property (strong,nonatomic)ACEDrawingView *DrawingViews;
#property (nonatomic, strong)UIScrollView *myScrollView;
in .m file
//creating the Drawing view
_DrawingViews = [[ACEDrawingView alloc] initWithFrame:CGRectMake(myOrigin, 0, self.testView.frame.size.width, self.testView.frame.size.height)];
_DrawingViews.drawTool=ACEDrawingToolTypePen;
_DrawingViews.tag=i;
_DrawingViews.delegate=self;
[myScrollView addSubview:_DrawingViews];
After Inside Copy method.
-(void)copy:(id)sender{
ACEDrawingView *dragView = [self.myScrollView viewWithTag:currentTag];// Getting ACEDrawing View with tag
CGSize size = [_userResizableView1 bounds].size;//just for size
UIGraphicsBeginImageContext(size);
[[dragView layer] renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
CGContextRef context=UIGraphicsGetCurrentContext();
[image drawInRect:CGRectMake(200,200, 145, 150)];
UIGraphicsEndImageContext();
}
But, I can paste it only as an image using UIImageView. I don't know how to paste it as a painting overview then only I can erase or redraw.
-Gratefull to Mr.Duncan sir and all who will answer.
UIImage has various drawing methods like drawInRect. You've already figured out how to create a graphics context. You could use drawInRect To draw your image into the graphics context.
(Note that you want to use the longer form of begin image context that takes a scale and options. (I'm not at my Mac to look up the function name.) If you pass in a scale of 0 it creates a Retina context on Retina devices. With the code you're using it will degrade retina images to non-retina resolution.)

Why does my programmatically created screenshot look so bad on iOS 7?

I am trying to implement sharing app with facebook.
I used this code to take the screenshot:
CGSize imageSize = CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height);
UIGraphicsBeginImageContext(imageSize);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
It works great on iOS 6, but in iOS 7 the image looks very bad.
I used this answer: iOS: what's the fastest, most performant way to make a screenshot programmatically?
for trying to fix it, and it helped, still the screenshot looks bad.
The screen gets another color, and some objects (like labels) aren't showing on the image taken.
Any help?
----Update----
I managed to solve the most objects, by change them to retain instead of weak. My main problem remained my tableview, that shown as a big white block (It supposed to be transparent, with labels with white text, so all we see is white cells). I did try to define the table background as clearcolor,not helps..
----Last Update---
There are wonderful answers here that not really regarding to my issue.. I wanted to make it work on device that runs with iOS7 but without using iOS7 SDK, since it takes to much effort to switch the project SDK in this point, when the project is almost done.
Anyway, I added the peace of code that finally solved my issue:
This change simply solve the problem:
UIGraphicsBeginImageContextWithOptions(imageSize, NO , 0.0f);
instead of:
UIGraphicsBeginImageContext(imageSize);
New API has been added since iOS 7, that should provide efficient way of getting snapshot
snapshotViewAfterScreenUpdates: renders the view into a UIView with unmodifiable content
resizableSnapshotViewFromRect:afterScreenUpdates:withCapInsets : same thing, but with resizable insets
drawViewHierarchyInRect:afterScreenUpdates: : same thing if you need all subviews to be drawn too (like labels, buttons...)
You can use the UIView returned for any UI effect, or render in into an image like you did if you need to export.
I don't know how good this new method performs VS the one you provided (although I remember Apple engineers saying this new API was more efficient)
you can try this
- (UIImage *) screenshot {
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, [UIScreen mainScreen].scale);
[self.view drawViewHierarchyInRect:self.view.bounds afterScreenUpdates:YES];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
in iOS 8 : this is how i am doing to get ScreenShot
just added one UIImageView and Method to take screenshot in .h file
#property (weak, nonatomic) IBOutlet UIImageView *imageView;
-(IBAction)takeSnapShot:(id)sender;
2 added code snip for taking screen shot and set on UIImageView in .m file
- (IBAction)takeSnapShot:(id)sender
{
UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *snapShotImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
imageView.image = snapShotImage;
}
below is the output i got.
On iOS7 you can have glitches if you use
[view drawViewHierarchyInRect:view.bounds afterScreenUpdates:YES]
during ongoing animation. Set afterScreenUpdates = NO to get rid of glitches.
Make sure that opaque is set to NO

Instantiate an image to appear at several different points on the screen?

I have an image called Empty.png, it is a small-ish square tile, how could I instantiate the image to appear at several different points on the screen?
Thank you for any help in advance :)
You can can place UIImageView's wherever you want the image to appear.And then set the image property of each image view as this image. (UIImage object).
If you are using interface builder then you just have to type in the name of the file in the attributes inspector of the imageview in the interface builder.
Or you could do this:
UIImage *img = [UIImage imageName:#"Empty.png"];
imageView.image = img; //Assuming this is your utlet to the image view.
It depends on how you want to use it.
If you just draw it with core graphics, let's say in drawInRect: or so, then you simply draw it several times.
If you want to display it within one or a number of image views, then instanciate your UIImageViews and assign the same object to all of them. Or let the Interface Builder do the instanciation for you. But you cannot add a single UIView object several times to one or a number of subview-hierarchies. If you add a UIView as subview to a view then it will disappear from the position where it was before.
10 UIImageView may use the same UIView but you need 10 UIImageViews to display all of them.
The same applies to UIButtons and every UI-thing that has an image or background image.
this will get you one image into some view
CGPoint position = CGPointMake(x,y);
UIImageView *img = [[UIImageView alloc] init];
img.image = [UIImage imageNamed:#"Empty.png"];
img.frame = CGRectMake(position.x, position.y, img.image.size.width, img.image.size.height);
[someUIView addSubview:img];
if you make an array for the positions (x,y) of all the images, then you can just run it in a for loop and it will place the images into the view at the positions you want
note: CGPoints cant be stored in an NSArray since its not an NSObject type, either use a C/C++ array or use something else that can fit into a NSArray

Combining two images from uiimageviews into one uiimage with correct positions

I have a view with two UIImageViews. The first uiimageview is a picture, which can be changed but is always at the same position. The second uiimageview contains a uiimage of a logo, this uiimageview can be resized, panned and rotated.
Both uiimageviews are set to Aspect Fit by the way.
When im done dragging and scaling the second uiimageview, i want to save these two uiimages, and draw one single uiimage. But in the new uiimage i need them to be positioned exactly as they were after i finished dragging and resizing the second uiimageview.
Im pushing this combined uiimage over to a new uiviewcontroller, which is called PreviewViewController. Here i want to show the uiimage in an imageview, and save it if the user presses yes.
I almost got it working, but the x and y position is confusing me.
And there is another problem, when drawin the second uiimageview image onto the new image, its view mode looks like Scale to fill and it looks ugly.
Heres my code
- (UIImage *)combineImages{
UIImage *tshirt = self.tskjorteTemplateView.image;
UIImage *logo = self.bildeView.image;
UIGraphicsBeginImageContext(self.tskjorteTemplateView.image.size);
UIGraphicsBeginImageContextWithOptions(self.tskjorteTemplateView.frame.size, NO, 0.0);
[tshirt drawInRect:CGRectMake(0,0, self.tskjorteTemplateView.frame.size.width, self.tskjorteTemplateView.frame.size.height)];
[logo drawInRect:CGRectMake(bildeView.center.x - (bildeView.frame.size.width / 2) ,
bildeView.center.y - (bildeView.frame.size.height / 2), self.bildeView.frame.size.width, self.bildeView.frame.size.height)];
UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return result;
}
and its called here:
fullPreviewImage = [self combineImages];
WantToSaveViewController *save = (WantToSaveViewController *)segue.destinationViewController;
save.delegate = [self.navigationController.viewControllers objectAtIndex:0];
save.previewImage = fullPreviewImage;

Merge two UIView into one UIImage

I've got two UIImageView: the first one is laying on the top of the other (eg. an overlay).
I want now to take a screenshot of the whole thing.
Note that before that step, I allow the user to change the overlay by panning,scaling and ROTATING it, so I must keep track of his editing.
So, here's the homework:
rotating the context basing on the view's transform rotation value
positioning on the origin, where the user finished to pan the overlay
calculate the size of the overlay view (it's always a rectangle, however!)
I'm gonna merge them inside a similar piece of code:
UIGraphicsBeginImageContext...
...
UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
but... what does "best fit" instead of the "..."?
Example code is well accepted!
Thanks
UIGraphicsBeginImageContext(firstImage.size);
[firstImage drawAtPoint:CGPointMake(0,0)];
[secondImage drawAtPoint:CGPointMake(0,0)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

Resources