ios - 如何在图片上写文字并保存

标签 ios objective-c uiview ios7 uiimage

我尝试先上传一张图片,然后在上面写一段文字,然后将这两个项目保存为一张图片。 我是 Objective C 的新手,所以我的代码可能不是实现目标的最佳方式,任何帮助和建议都会对我有所帮助。 谢谢你们 这是我的代码:

这个编辑器为什么不允许我把我的方法声明放在 在这里输入代码

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)];

这是另一个方法声明同样的问题编辑不允许我把它放在

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;

最佳答案

我真的不知道是什么问题,但我这样做了..让我知道它是否适合你

- (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];
}

关于ios - 如何在图片上写文字并保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20961088/

相关文章:

IOS后台定位服务,因时而异

javascript - 点击按钮时解析云代码未运行

ios - 禁用单个 UIImageView 的自动布局

ios - 带有文本长度计数器的 UITextField

objective-c - 从 NSString 中删除字符?

objective-c - 在 Cocoa Touch 中读取 .ppt (MS PowerPoint) 文件

ios - 定义 UIViewController 或 UIView 的自定义框架

ios - 创建子 UIView 的 UIView 正在生成层次结构约束警告

iphone - 查看一切之上 : UIWindow subview VS UIViewController subview

ios - Swift - 按下时为按钮创建径向线