2010-07-27 40 views
1

我使用以下代碼獲取屏幕截圖並將其保存到相冊中。UIGetScreenImage()在景觀?

CGImageRef screen = UIGetScreenImage(); 
     UIImage *screenImage = [UIImage imageWithCGImage:screen]; 
    UIImageWriteToSavedPhotosAlbum(screenimage, nil, nil), nil); 

這很好用,但如果我的應用程序處於橫向模式,則保存的圖像不會正確顯示。 (需要90度轉彎)。我需要做什麼?

+2

請注意,UIGetScreenImage()已被Apple重新制作,如果使用此功能,您的應用程序將被拒絕:http://www.drahtwerk.biz/CN/Blog.aspx/iOS-4-updates-延遲/?newsID = 45 – 2010-07-27 12:32:13

回答

2

試驗要麼

[UIDevice currentDevice].orientation == UIInterfaceOrientationLandscapeRight 
[UIDevice currentDevice].orientation == UIInterfaceOrientationLandscapeLeft 
(and possibly more orientations) 

然後做相應的轉變到圖像。

這可以通過幾種方式來實現,這其中並不需要很多代碼:

/// click action saves orientation & picture: 
-(void)screenshot 
{ 
    self.orientationWhenPicked = [UIDevice currentDevice].orientation; 
    CGImageRef screen = UIGetScreenImage(); 
    self.image = [UIImage imageWithCGImage:screen]; 
    CGImageRelease(screen); 
} 

/// somewhere else we are called for the image 
-(UIImage*)getScreenshot 
{ 
    UIDeviceOrientation useOrientation = self.orientationWhenPicked; 
    UIImage *img = self.image; 

    UIGraphicsBeginImageContext(CGSizeMake(480.0f, 320.0f)); 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    if (useOrientation == UIDeviceOrientationLandscapeRight) 
    { 
      CGContextRotateCTM (context, M_PI/2.0f); 
      [img drawAtPoint:CGPointMake(0.0f, -480.0f)]; 
    } else { 
      CGContextRotateCTM (context, -M_PI/2.0f); 
      [img drawAtPoint:CGPointMake(-320.0f, 0.0f)]; 
    } 
    UIImage *ret = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return ret; 
} 

我修改了一些張貼,使其自成一體之前,但它應該有一個工作一點努力(例如創建self.image等)。你可以將它們混合成一個函數。

+0

非常感謝!你能解釋一下這條線是什麼:[img drawAtPoint:CGPointMake(-320.0f,0.0f)]; 它與旋轉有什麼關係? 在此先感謝, -David – bobbypage 2010-08-05 07:08:45

+0

這是將原始圖像放入新圖像上下文中的線條。 '(-320,0)'座標確保在繞(0,0)旋轉後覆蓋整個屏幕。 – mvds 2010-08-05 10:31:39

+0

這兩個函數中的self.image是什麼?爲什麼getScreenshot是一個無效的方法? – malhal 2012-01-24 01:17:43