【发布时间】:2012-04-30 15:28:44
【问题描述】:
我正在开发一个应用程序,用户可以在图像上写一个标签(文本),并且必须将其与标签一起保存在 iphone 本地存储中。
(即)用户可以在图像上提供的位置添加任何文本,并且必须将其保存为带有他在 iphone 本地存储中写入的标签的图像
提前致谢
【问题讨论】:
-
您是否已经尝试过?然后请发布一些代码
标签: iphone objective-c xcode image ipad
我正在开发一个应用程序,用户可以在图像上写一个标签(文本),并且必须将其与标签一起保存在 iphone 本地存储中。
(即)用户可以在图像上提供的位置添加任何文本,并且必须将其保存为带有他在 iphone 本地存储中写入的标签的图像
提前致谢
【问题讨论】:
标签: iphone objective-c xcode image ipad
我认为您需要将 UIImageView 和 UILabel 添加为特定 UIView 的子视图(假设我们将此视图称为 customView),然后使用该视图截取屏幕截图
how to take a screenshot of the iPhone programmatically?
从修改后的链接中回答,以便您更容易理解,如下:
重要提示:添加QuartzCore Framework和添加#import<QuartzCore/QuartzCore.h>
CGSize screenSize = [[UIScreen mainScreen] applicationFrame].size; CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB(); CGContextRef ctx = CGBitmapContextCreate(nil, screenSize.width, screenSize.height, 8, 4*(int)screenSize.width, colorSpaceRef, kCGImageAlphaPremultipliedLast); CGContextTranslateCTM(ctx, 0.0, screenSize.height); CGContextScaleCTM(ctx, 1.0, -1.0); [(CALayer*)customView.layer renderInContext:ctx]; CGImageRef cgImage = CGBitmapContextCreateImage(ctx); UIImage *image = [UIImage imageWithCGImage:cgImage]; CGImageRelease(cgImage); CGContextRelease(ctx); [UIImageJPEGRepresentation(image, 1.0) writeToFile:filePath atomically:NO];这里的filePath是你的应用程序的Documents目录文件路径。
您可以使用以下方式获取您的 NSDocuments 目录文件路径:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"savedImage.png"];
现在在您希望捕获视图的任何按钮或其他控件单击上执行此代码。
如果您需要更多帮助,请告诉我。
希望对你有所帮助。
【讨论】:
您可以将 UILabel 作为子视图添加到包含 UIImage 的 UIImageView 那么
UIGraphicsBeginImageContextWithOptions(UIImageView.bounds.size, UIImageView.opaque, 0.0);
[UIImageView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
你得到你想要的 UIImage
或
自己画出来
UIGraphicsBeginImageContext(imageSize);
......
CGContextDrawImage(.....);
CGContextShowTextAtPoint(.....);
......
UIGraphicsEndImageContext();
【讨论】:
您需要捕获整个屏幕的屏幕截图,以便将两者都放在一张图像中。
Apple 文档中的更多信息here
【讨论】: