ios saving uilabel and uiimageview as uiimage
- by Ashraf Hussein
I'm trying to add text to an image bu 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);
THX