简体   繁体   English

Xcode 4.3如何保存应用程序屏幕的区域?

[英]Xcode 4.3 How do I save a region of my app screen?

I am creating a native app on Xcode 4.3 and deploying to target iOS 5. The app is a basically a greeting card creator. 我正在Xcode 4.3上创建本机应用程序,并部署到目标iOS5。该应用程序基本上是贺卡创建者。 I am having trouble figuring out how to save a portion of the screen from within the app. 我在弄清楚如何从应用程序中保存屏幕的一部分时遇到了麻烦。

What I want to do is this: 我想做的是这样的:

I am offering the user a button, that says "e-mail". 我为用户提供了一个按钮,上面写着“电子邮件”。 when they click the button, the app should 'save' their card as an image and then 'paste' that into an email body. 当他们单击按钮时,应用程序应将其卡片“保存”为图像,然后将其“粘贴”到电子邮件正文中。

The reason this is different than other answers on this website is that the area I want to save is made up of 4 'elements'. 这与本网站上其他答案不同的原因是我要保存的区域由4个“元素”组成。 There is a background graphic that is the tilted card background, then there is a text field where users can type a message and then next to that is a picture area where they can choose their own picture to put on the card. 有一个背景图形,它是倾斜的卡片背景,然后是一个文本字段,用户可以在其中键入消息,然后是一个图片区域,在该图片区域中,他们可以选择自己的图片以放在卡片上。

Here is a photo of what I am talking about: http://marklopezdesigns.com/mydownloadz!/screenshotCard3.png 这是我在说什么的照片: http : //marklopezdesigns.com/mydownloadz!/screenshotCard3.png

How do I save a 'composite' high res of these? 如何保存这些“复合”高分辨率?

And then how do I get that into an email body message? 然后,如何将其转化为电子邮件正文?

The reason i am asking how to 'save' it is because I want to be able to offer users another button that says "save to camera roll" and "send as message". 我询问如何“保存”的原因是因为我希望能够向用户提供另一个按钮,该按钮显示“保存到相机胶卷”和“作为消息发送”。 I figure if I can understand how to save this high-res to a variable, then I should be off and running. 我认为,如果我能理解如何将高分辨率保存到变量中,那么我应该关闭并运行。

Thanks in advance for the help. 先谢谢您的帮助。

======== ========

Here's the solution below 这是下面的解决方案

======== ...so after a bit of fiddling. ========= ...经过一番摆弄。 Finally got what I wanted. 终于得到了我想要的。 Here's the codebase I have in my method that fires upon touch of the "Save to Album" button: 这是我在方法中拥有的代码库,只需触摸“保存到相册”按钮即可触发:

- (IBAction)savePhoto{  

CGRect rect;  
rect = CGRectMake(11,50 ,305, 262);  
UIView *cardViewer = [[UIView alloc] initWithFrame:rect];  
UIGraphicsBeginImageContext(cardViewer.bounds.size);   
//make view background transparent  
cardViewer.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.0];  
cardViewer.opaque = NO;  

//stuff items into a subview for capturing  
[self.view addSubview:cardViewer];  
[cardViewer addSubview:self.tiltedCard];  
[cardViewer addSubview:self.bigCardView];  
[cardViewer addSubview:self.cardWords];  
[cardViewer addSubview:self.photoView];  

[cardViewer.layer renderInContext:UIGraphicsGetCurrentContext()];  
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();  
UIGraphicsEndImageContext();  
CGImageRef imageRef = CGImageCreateWithImageInRect([viewImage CGImage], rect);  
UIImage *img = [UIImage imageWithCGImage:imageRef];  
CGImageRelease(imageRef);  
UIImageWriteToSavedPhotosAlbum(img, nil, nil, nil);  

//put everything back where it belongs  
[self.view addSubview:self.tiltedCard];  
[self.view addSubview:self.bigCardView];  
[self.view addSubview:self.cardWords];  
[self.view addSubview:self.photoView];  

[cardViewer removeFromSuperview];  

} }

To capture just an area of the screen, specify the bounds using CGRectMake . 要仅捕获屏幕区域,请使用CGRectMake指定边界。

CGRect rect = CGRectMake(50, 50, 100, 100);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[self.view.layer renderInContext:context];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

In the example above we are capturing a 100px by 100px region beginning at x:50px and y:50px. 在上面的示例中,我们捕获了一个以x:50px和y:50px开始的100px x 100px区域。

maybe get the picture of the layer by rendering the layer context and grabbing the image, I use this for displaying fancy animations 也许通过渲染图层上下文并获取图像来获取图层图片,我用它来显示精美的动画

UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

To capture just an area of the screen, use the UIView or SubView and just specify the bounds using CGRectMake. 要仅捕获屏幕区域,请使用UIView或SubView并仅使用CGRectMake指定边界。

CGRect rect = CGRectMake(10, 10, 200, 200);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[self.YourView.layer renderInContext:context]; // Make sure here you are using the  greetingcardView rather than self.view
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

Note: self.view will capture the whole screen so simply render the view wherever. 注意: self.view将捕获整个屏幕,因此只需在任何位置渲染视图即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM