I'm trying to add text to an image but adding a uilabel as subview to a uiimageview.
I already did that but I want to save them as an image;
I'm using render in context but it's not working.
here's my code:
UIImage * img = [UIImage imageNamed:#"IMG_1650.JPG"];
float x = (img.size.width/imageView.frame.size.width) * touchPoint.x;
float y = (img.size.height/imageView.frame.size.height) * touchPoint.y;
CGPoint tpoint = CGPointMake(x, y);
UIFont *font = [UIFont boldSystemFontOfSize:30];
context = UIGraphicsGetCurrentContext();
UIGraphicsBeginImageContextWithOptions(img.size, YES, 0.0);
[[UIColor redColor] set];
for (UIView * view in [imageView subviews]){
[view removeFromSuperview];
}
UILabel * lbl = [[UILabel alloc]init];
[lbl setText:txt];
[lbl setBackgroundColor:[UIColor clearColor]];
CGSize sz = [txt sizeWithFont:lbl.font];
[lbl setFrame:CGRectMake(touchPoint.x, touchPoint.y, sz.width, sz.height)];
lbl.transform = CGAffineTransformMakeRotation( -M_PI/4 );
[imageView addSubview:lbl];
[imageView bringSubviewToFront:lbl];
[imageView setImage:img];
[imageView.layer renderInContext:UIGraphicsGetCurrentContext()];
[lbl.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * nImg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(nImg, nil, nil, nil);
Try the code below. It works for me.
UIGraphicsBeginImageContext(imageView.bounds.size);
[imageView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *bitmap = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
P.S. I think you don't need to renderInContext UILabel layer.
Related
I need round and text on the round on UIImage like this Image
Sample Image here
Here is my code which i achieved already but with square not in round, i need round around the text.
+(UIImage*)drawText:(NSString*)text inImage:(UIImage*)image atPoint:(CGPoint)point
{
UIFont *font = [UIFont boldSystemFontOfSize:50];
UIGraphicsBeginImageContext(image.size);
[image drawInRect:CGRectMake(0,0,image.size.width,image.size.height)];
CGRect rect = CGRectMake(point.x - 10, point.y - 10, image.size.width, image.size.height);
[text drawInRect:CGRectIntegral(rect) withAttributes:#{NSFontAttributeName:font,NSBackgroundColorAttributeName:[UIColor colorWithRed:0.26 green:0.32 blue:0.59 alpha:1.0],NSForegroundColorAttributeName:[UIColor whiteColor]}];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
Thanks in advance.
Do something like that:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.imgview.image = [ViewController drawText:#"iOS" inImage:[UIImage imageNamed:#"eye"] atPoint:CGPointMake(30 , 30)];
self.imgview.layer.cornerRadius = self.imgview.frame.size.height/2;
self.imgview.clipsToBounds = YES;
}
+ (UIImage*)drawText:(NSString*)text inImage:(UIImage*)image atPoint:(CGPoint)point
{
UIFont *font = [UIFont boldSystemFontOfSize:20];
UIGraphicsBeginImageContext(image.size);
[image drawInRect:CGRectMake(0,0,image.size.width,image.size.height)];
CGRect rect = CGRectMake(point.x - 10, point.y - 10, image.size.width, image.size.height);
[text drawInRect:CGRectIntegral(rect) withAttributes:#{NSFontAttributeName:font,NSBackgroundColorAttributeName:[UIColor colorWithRed:0.26 green:0.32 blue:0.59 alpha:1.0],NSForegroundColorAttributeName:[UIColor whiteColor]}];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
UPDATE
For this you have to add some more logic like below.
- (void)viewDidLoad {
[super viewDidLoad];
self.imgview.image = [ViewController drawText:#"iOS Dev" inImage:[UIImage imageNamed:#"Lion"] bgCircleRect:CGRectMake(15, 15, 100, 100)];
}
+ (UIImage*)drawText:(NSString*)text inImage:(UIImage*)image bgCircleRect:(CGRect) bgCircleRect
{
UIFont *font = [UIFont boldSystemFontOfSize:20];
UIGraphicsBeginImageContext(image.size);
[image drawInRect:CGRectMake(0,0,image.size.width,image.size.height)];
CGRect rect = CGRectMake(bgCircleRect.origin.x+10, bgCircleRect.origin.y+35, bgCircleRect.size.width, bgCircleRect.size.height);
UIBezierPath* path = [UIBezierPath bezierPathWithRoundedRect: bgCircleRect cornerRadius: 50];
[UIColor.redColor setFill];
[path fill];
[text drawInRect:CGRectIntegral(rect) withAttributes:#{NSFontAttributeName:font,NSBackgroundColorAttributeName:[UIColor redColor],NSForegroundColorAttributeName:[UIColor whiteColor]}];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
Output
For making the UIImageView round, First check whether the length and width is same, then apply the following code in the viewDidLoad()
yourpic.layer.cornerRadius = yourpic.frame.size.width / 2;
yourpic.clipsToBounds = YES;
I think this will do.
I think you are looking for corner radius :
+(void)drawText:(NSString*)text inImage:(UIImage*)image atPoint:(CGPoint)point{
UIImageView *imageView = [[UIImageView alloc] init];
UIFont *font = [UIFont boldSystemFontOfSize:50];
UIGraphicsBeginImageContext(image.size);
[image drawInRect:CGRectMake(0,0,image.size.width,image.size.height)];
CGRect rect = CGRectMake(point.x - 10, point.y - 10, image.size.width, image.size.height);
[text drawInRect:CGRectIntegral(rect) withAttributes:#{NSFontAttributeName:font,NSBackgroundColorAttributeName:[UIColor colorWithRed:0.26 green:0.32 blue:0.59 alpha:1.0],NSForegroundColorAttributeName:[UIColor whiteColor]}];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
imageView.center = point;
imageView.image = newImage; }
Add image to imageView and add that as subView to your main view
How can I call this function?
I am trying to call this function from -viewDidLoad.
I tried [circularImageWithImage(imageView.image, myclor, 0.2)];
static UIImage *circularImageWithImage(UIImage *inputImage,
UIColor *borderColor,
CGFloat borderWidth)
{
CGRect rect = (CGRect){ .origin=CGPointZero, .size=inputImage.size };
UIGraphicsBeginImageContextWithOptions(rect.size, NO, inputImage.scale); {
// Fill the entire circle with the border color.
[borderColor setFill];
[[UIBezierPath bezierPathWithOvalInRect:rect] fill];
// Clip to the interior of the circle (inside the border).
CGRect interiorBox = CGRectInset(rect, borderWidth, borderWidth);
UIBezierPath *interior = [UIBezierPath bezierPathWithOvalInRect:interiorBox];
[interior addClip];
[inputImage drawInRect:rect];
}
UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return outputImage;
}
You can try with this code.
UIImage *image = [UIImage imageNamed:#"yourImage.png"];
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];//set your frame
//imageView.center = self.view.center;
UIImage *modifiedImage = circularImageWithImage(image, [UIColor redColor], 1.2);//border width >= 1.0 is better. Otherwise you may not see this
imageView.image = modifiedImage;
[self.view addSubview:imageView];
UIImage *sample = circularImageWithImage(imageView.image, myclor, 0.2);
I create a UIImageView with two UILabel as subviews of it:
UIImageView *img=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, [[UIScreen mainScreen] bounds].size.height)];
img.backgroundColor=self.view.backgroundColor;
UILabel *textOne=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 290, 250)];
textOne.text=_textOneLabel.text;
textOne.textColor=_textOneLabel.textColor;
UILabel *textTwo = [[UILabel alloc] initWithFrame:CGRectMake(100,180,200,100)];
textTwo.text=_textTwoLabel.text;
textTwo.textColor=_textTwoLabel.textColor;
[img addSubview:textOne];
[img addSubview:textTwo];
UIImageWriteToSavedPhotosAlbum(img.image, nil, nil, nil); //img.image is null
I want to create a UIImage to save it in the camera roll with the UIImageView and the 2 created UILabel. img.image returns null because there is no image assigned to the UIImageView.
Do you have an idea of how achieving that?
Use below method to convert your UIImageView to UIImage.
UIImage *image = [self ChangeImageViewToImage:img];
//Method
-(UIImage *) ChangeImageViewToImage : (UIImageView *) view{
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
This should help you
/* Creates an image with a home-grown graphics context, burns the supplied string into it. */
- (UIImage *)burnTextIntoImage:(NSString *)text :(UIImage *)img {
UIGraphicsBeginImageContext(img.size);
CGRect aRectangle = CGRectMake(0,0, img.size.width, img.size.height);
[img drawInRect:aRectangle];
[[UIColor redColor] set]; // set text color
NSInteger fontSize = 14;
if ( [text length] > 200 ) {
fontSize = 10;
}
UIFont *font = [UIFont boldSystemFontOfSize: fontSize]; // set text font
[ text drawInRect : aRectangle // render the text
withFont : font
lineBreakMode : UILineBreakModeTailTruncation // clip overflow from end of last line
alignment : UITextAlignmentCenter ];
UIImage *theImage=UIGraphicsGetImageFromCurrentImageContext(); // extract the image
UIGraphicsEndImageContext(); // clean up the context.
return theImage;
}
I have a UITextView and an UIImageView that I want to convert into a single image to share.
SaveImageView, is the ImageView, where I want to save the image of the textview.
The Textview can move it across the screen, so I decide to save their final position and give it to the SaveImageView.
First convert the UItextView in an image and save his position.
Then, I want to join two imageview, into a single image.
self.SaveTextView.frame = self.textView.frame;
UIGraphicsBeginImageContext (self.textView.bounds.size);
[self.textView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.SaveTextView.image=resultingImage;
//join two uiimageview
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, 0.0);
[self.BaseImageView.image drawInRect:CGRectMake(0, 0, self.BaseImageView.frame.size.width, self.BaseImageView.frame.size.height)];
[self.SaveTextView.image drawInRect:CGRectMake(self.SaveTextView.frame.origin.x, self.SaveTextView.frame.origin.y, self.SaveTextView.frame.size.width, self.SaveTextView.frame.size.height)];
_SaveImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
The problem is that the place where I write in the textview is not in the same position of the saved image, the image with the text is slightly moved, when it should be the exact spot where I have written, not find where is the error.
Any Help ??
Try implement this method and call it on superview:
#implementation UIView (ScreenShot)
- (UIImage *)screenShot
{
UIGraphicsBeginImageContextWithOptions(self.bounds.size, self.opaque, 0.0);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
#end
Try this...
//Make Label
UILabel *_labelTimeOutName = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 428, 32)];
_labelTimeOutName.text = #"Your label text here";
_labelTimeOutName.minimumScaleFactor = 0.5;
_labelTimeOutName.numberOfLines = 1;
_labelTimeOutName.textColor = [UIColor whiteColor];
_labelTimeOutName.backgroundColor = [UIColor clearColor];
_labelTimeOutName.font = [UIFont fontWithName:#"TrebuchetMS-Bold" size:24];
_labelTimeOutName.textAlignment = NSTextAlignmentCenter;
//add label inside a temp View
UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 428, 32)];
tempView.backgroundColor = [UIColor clearColor];
[tempView addSubview:_labelTimeOutName];
//render image
//CGContextRef context = UIGraphicsGetCurrentContext();
UIGraphicsBeginImageContextWithOptions(tempView.bounds.size, tempView.opaque, 0.0);
// The view to be rendered
[[tempView layer] renderInContext:UIGraphicsGetCurrentContext() ]; //context];
// Get the rendered image
//////////////////////////////////////////////////////////////////////
UIImage *FinalTextIamge = UIGraphicsGetImageFromCurrentImageContext();
//////////////////////////////////////////////////////////////////////
UIGraphicsEndImageContext();
Enjoy!
I'm trying to create a UIbutton that has the following characteristics:
* A custom image as its content
* Rounded corners
* A drop shadow
Using the following code will format the button to appear correctly and function but the highlight action (i.e., the darkening of the button when touched) is lost when the touch occurs. Any ideas?
+(void) styleButton:(UIButton*)button{
UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(button.bounds.origin.x, button.bounds.origin.y, button.bounds.size.width, button.bounds.size.height)];
CALayer *sublayer = [CALayer layer];
sublayer.shadowOffset = CGSizeMake(0, 3);
sublayer.shadowRadius = 5.0;
sublayer.shadowColor = [UIColor blackColor].CGColor;
sublayer.shadowOpacity = 0.8;
sublayer.frame = CGRectMake(button.bounds.origin.x, button.bounds.origin.y, button.bounds.size.width, button.bounds.size.height);
backgroundView.userInteractionEnabled = NO;
[backgroundView.layer addSublayer:sublayer];
[button insertSubview:backgroundView atIndex:0];
CALayer *imageLayer = [CALayer layer];
imageLayer.frame = sublayer.bounds;
imageLayer.cornerRadius = 10.0;
imageLayer.contents = (id) [UIImage imageNamed:#"myImage.png"].CGImage;
imageLayer.cornerRadius = 5.0f;
imageLayer.masksToBounds = YES;
[sublayer addSublayer:imageLayer];
}
Have you tried converting your backgroundView into a UIImage and add it to the button instead of adding backgroundView as a subview? That would preserve the darkening when the button is pressed:
UIGraphicsBeginImageContext(backgroundView.frame.size);
[backgroundView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[button setImage:image forState:UIControlStateNormal];
Like this (ARC enabled):
UIGraphicsBeginImageContextWithOptions(self.button.bounds.size, NO, [[UIScreen mainScreen] scale]);
CGContextRef context = UIGraphicsGetCurrentContext();
for (UIView *aView in self.button.subviews) {
CGContextSaveGState(context);
CGContextTranslateCTM(context, aView.frame.origin.x, aView.frame.origin.y);
[aView.layer renderInContext:UIGraphicsGetCurrentContext()];
CGContextRestoreGState(context);
}
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[self.button setImage:image forState:UIControlStateNormal];
[self.button.subviews makeObjectsPerformSelector:#selector(removeFromSuperview)];
[self.button setAdjustsImageWhenHighlighted:YES];