2011-04-13 54 views
1

我可以以某種方式以編程方式製作一個ImageView的打印屏幕嗎?iPhone imageview打印屏幕 - 從imageview創建新圖像

假設我正在顯示不同尺寸的大照片,自動縮放(AspectFit)爲屏幕大小,居中且背景爲黑色。我想捕捉包含黑色圖像視圖背景的縮放圖像,以便使用320x480圖像。

那麼如何將例如350x600圖像更改爲320x480黑色邊框,並且沒有任何其他元素(按鈕,標籤)覆蓋此圖像視圖?在Android上我只是捕獲屏幕緩衝區,但對於iPhone我真的不知道該怎麼做...

在此先感謝!

+0

茹試圖讓scren拍攝 – 2011-04-13 09:20:13

回答

5
UIGraphicsBeginImageContext(<specify your size here as CGRect>); 
[<yourImageView or view you want to convert in image>.layer renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); 
     UIGraphicsEndImageContext(); 

viewImage的輸出。因爲你想要一個黑色的邊框,所以你可以有一個這樣的邏輯,添加imageView UIView與blackBackroundColor黑色(記得這個視圖的框架應該比你的imageView的框架,每邊有一定的餘量,這樣你可以有你的邊框).....現在在[<yourView>.layer renderInContext:UIGraphicsGetCurrentContext()];使用這種方法。

+0

關於此邊框我的意思在全屏圖像視圖中縮放圖像時的黑色背景。無論哪種方式,當我保存UIImage文件它是白色和空的:( – yosh 2011-04-13 13:12:41

1
UIGraphicsBeginImageContextWithOptions(CGSizeMake(320, 480), NO, 0.0); 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    [self.view.layer renderInContext:context]; 
    yourImageViewToStoreCaptured = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

希望這會有所幫助。

+0

保存的UIImage * yourImageViewToStoreCaptured在空白8 KB文件磁盤結果:(我捕捉self.imageview.layer。 – yosh 2011-04-13 13:10:18

+0

好了,解決了,THX :) – yosh 2011-04-13 13:39:39

1
-(IBAction)saveViewToPhotoLibrary:(id)sender 
{ 
    CGRect screenRect = [[UIScreen mainScreen] bounds]; 
    UIGraphicsBeginImageContext(screenRect.size); 

    CGContextRef ctx = UIGraphicsGetCurrentContext(); 
    [[UIColor blackColor] set]; 
    CGContextFillRect(ctx, screenRect); 

    [self.view.layer renderInContext:ctx]; 

    UIImage *screenImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIImageWriteToSavedPhotosAlbum(screenImage,nil,nil,nil);           
    //fill cordinat in place of nil if u want only image comes and not button......ok thanks vijay// 
    UIGraphicsEndImageContext();  
}