簡體   English   中英

Xcode 4.3如何保存應用程序屏幕的區域?

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

我正在Xcode 4.3上創建本機應用程序,並部署到目標iOS5。該應用程序基本上是賀卡創建者。 我在弄清楚如何從應用程序中保存屏幕的一部分時遇到了麻煩。

我想做的是這樣的:

我為用戶提供了一個按鈕,上面寫着“電子郵件”。 當他們單擊按鈕時,應用程序應將其卡片“保存”為圖像,然后將其“粘貼”到電子郵件正文中。

這與本網站上其他答案不同的原因是我要保存的區域由4個“元素”組成。 有一個背景圖形,它是傾斜的卡片背景,然后是一個文本字段,用戶可以在其中鍵入消息,然后是一個圖片區域,在該圖片區域中,他們可以選擇自己的圖片以放在卡片上。

這是我在說什么的照片: http : //marklopezdesigns.com/mydownloadz!/screenshotCard3.png

如何保存這些“復合”高分辨率?

然后,如何將其轉化為電子郵件正文?

我詢問如何“保存”的原因是因為我希望能夠向用戶提供另一個按鈕,該按鈕顯示“保存到相機膠卷”和“作為消息發送”。 我認為,如果我能理解如何將高分辨率保存到變量中,那么我應該關閉並運行。

先謝謝您的幫助。

========

這是下面的解決方案

========= ...經過一番擺弄。 終於得到了我想要的。 這是我在方法中擁有的代碼庫,只需觸摸“保存到相冊”按鈕即可觸發:

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

}

要僅捕獲屏幕區域,請使用CGRectMake指定邊界。

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

在上面的示例中,我們捕獲了一個以x:50px和y:50px開始的100px x 100px區域。

也許通過渲染圖層上下文並獲取圖像來獲取圖層圖片,我用它來顯示精美的動畫

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

要僅捕獲屏幕區域,請使用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();

注意: self.view將捕獲整個屏幕,因此只需在任何位置渲染視圖即可。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM