[英]how can i write a text on the image and save it
im trying to first upload an image then write a text on it and after that save both items as one image. 我试图先上传一张图片,然后在上面写上文字,然后将两项都保存为一张图片。 im new in objective c so my code might be not the best way to get to my goal any help and suggestion wil help me.
我在目标c中是新手,所以我的代码可能不是达到目标的最佳方法,任何建议和建议都会对我有所帮助。 thank you all this is my code :
谢谢,这就是我的代码:
some how this editor is not allowing me to put my method declaration on enter code here 一些此编辑器如何不允许我在此处输入代码的方法声明
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
NSData *data = UIImagePNGRepresentation(image);
NSString *mijnImage =@"mijnImage.png";
NSArray *route = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
NSString *imageRoute = [route objectAtIndex:0];
NSString *routeNaarDeBestaand = [imageRoute stringByAppendingPathComponent:mijnImage];
[data writeToFile:routeNaarDeBestaand atomically:YES];
[[self imageView]setImage:image];
[self dismissViewControllerAnimated:YES completion:Nil];
self.imageView.image = [self drawText:@"Test String" inImage:image atPoint:CGPointMake(10, 20)];
this is another methode declaration same problem the editor is not allowing me to put it on 这是另一个方法声明相同的问题,编辑器不允许我放
UIFont *font = [UIFont boldSystemFontOfSize:100];
UIGraphicsBeginImageContext(self.view.frame.size);
[originalImage drawInRect:CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height)];
CGRect rect = CGRectMake(point.x, point.y,lblText.frame.size.width, lblText.frame.size.height);
rect.size = [text sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:150.0f]}];
NSMutableDictionary *attributes = [[NSMutableDictionary alloc] init];
[attributes setObject:font forKey:NSFontAttributeName];
[attributes setObject:[NSNumber numberWithFloat:4] forKey:NSStrokeWidthAttributeName];
[attributes setObject:[UIColor whiteColor] forKey:NSStrokeColorAttributeName];
[text drawInRect:rect withAttributes:attributes];
[attributes removeObjectForKey:NSStrokeWidthAttributeName];
[attributes removeObjectForKey:NSStrokeColorAttributeName];
[attributes setObject:[UIColor blackColor] forKey:NSForegroundColorAttributeName];
[text drawInRect:rect withAttributes:attributes];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
I don't really know what problem is, but I did this.. let me know if it works for you 我真的不知道是什么问题,但是我做到了..让我知道它是否对您有用
- (void)viewDidLoad
{
UIImage *image = [UIImage imageNamed:@"image"];
UIImage *newImage = [self image:image withText:@"Hello World" atPoint:CGPointMake(20, 20)];
[self saveImage:image withName:@"original"];
[self saveImage:newImage withName:@"new"];
}
- (UIImage*)image:(UIImage*)image withText:(NSString*)text atPoint:(CGPoint)point
{
CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height);
UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
[image drawInRect:rect];
NSDictionary *attributes = @{NSFontAttributeName : [UIFont boldSystemFontOfSize:20],
NSStrokeWidthAttributeName : @(4),
NSStrokeColorAttributeName : [UIColor whiteColor]};
[text drawAtPoint:point withAttributes:attributes];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
- (void)saveImage:(UIImage*)image withName:(NSString*)name
{
NSString *documents = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSString *imagePath = [documents stringByAppendingPathComponent:name];
[UIImagePNGRepresentation(image) writeToFile:imagePath atomically:YES];
}
Try this 尝试这个
+(UIImage*) drawText:(NSString*) text
inImage:(UIImage*) image
atPoint:(CGPoint) point
{
UIFont *font = [UIFont boldSystemFontOfSize:12];
UIGraphicsBeginImageContext(image.size);
[image drawInRect:CGRectMake(0,0,image.size.width,image.size.height)];
CGRect rect = CGRectMake(point.x, point.y, image.size.width, image.size.height);
[[UIColor whiteColor] set];
[text drawInRect:CGRectIntegral(rect) withFont:font];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
This answer will make the text both aligned vertically and horizontally in the center of the UIImage. 这个答案将使文本在UIImage的中心垂直和水平对齐。 Note: not my answer it is collected from several answers
注意:不是我的答案,它是从几个答案中收集的
- (UIImage*)image:(UIImage*)image withText:(NSString*)text{
CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height);
UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
[image drawInRect:rect];
/// Make a copy of the default paragraph style
NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
/// Set line break mode
paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;
/// Set text horizontal alignment
paragraphStyle.alignment = NSTextAlignmentCenter;
NSDictionary *attributes = @{NSFontAttributeName : [UIFont boldSystemFontOfSize:16],
NSStrokeWidthAttributeName : @(4),
NSStrokeColorAttributeName : [UIColor whiteColor],
NSParagraphStyleAttributeName : paragraphStyle};
CGSize size = [text sizeWithAttributes:attributes];
CGRect newRect = CGRectMake(rect.origin.x,
rect.origin.y + (rect.size.height - size.height)/2.0,
rect.size.width,
size.height);
[text drawInRect:newRect withAttributes:attributes];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.