2012-04-19 27 views
2

我正在开发一个应用程序,用户可以在图像上写一个标签(文本),并将其保存在带有标签的iphone本地存储中。如何保存在iPhone上有标签的图像?

(IE)的用户可以添加任何的文字放在图像提供,并有将其保存为与他在iphone本地存储写标签图像

在此先感谢

+1

您是否尝试过某些东西?然后请张贴一些代码 – HeikoG 2012-04-19 06:18:36

回答

1

我认为你需要添加你的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]; 

这里文件路径是应用程序的Documents目录的文件路径。

可以使用让您的NSDocuments目录文件路径:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"savedImage.png"]; 

现在执行上的任何按钮或其他控件的代码单击要捕获的视图。

让我知道你是否需要更多帮助。

希望这可以帮助你。

+0

这段代码可以帮助我捕获屏幕。你能否告诉我如何捕获屏幕的特定部分 – 2012-04-19 06:51:39

+0

在你正在使用的屏幕的特定部分添加一个名为customView的视图,取得customView的IBOutlet,然后执行我在答案中给出的代码。请参阅我的答案中给出的代码,而不是在链接中,因为链接中的答案是关于整个屏幕捕获:)让我知道你是否需要更多帮助。 – 2012-04-19 07:03:59

+0

谢谢帕特巴特。你让我的工作更轻松。 – 2012-04-19 07:05:54

1

可以添加的UILabel作为一个子视图到的UIImageView包含的UIImage 然后

UIGraphicsBeginImageContextWithOptions(UIImageView.bounds.size, UIImageView.opaque, 0.0); 
[UIImageView.layer renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

你得到的UIImage你想

只是自己绘制它

UIGraphicsBeginImageContext(imageSize); 

...... 
CGContextDrawImage(.....); 
CGContextShowTextAtPoint(.....); 
...... 

UIGraphicsEndImageContext(); 
0

您需要将整个屏幕的屏幕截图捕获到一个图像中。

更多关于Apple的文档可用here