简体   繁体   中英

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. 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'. 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

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 .

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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