2013-10-07 67 views
1

下面是從模擬器截圖並保存圖像:UIImage的節能問題iOS7:混合問題

enter image description here

編號:

UIGraphicsBeginImageContext(self.view.bounds.size); 

[self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; 

UIImage *screenshotImage = UIGraphicsGetImageFromCurrentImageContext(); 

UIGraphicsEndImageContext(); 

//guru - just for simulator 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"Image.png"]; 

[UIImagePNGRepresentation(screenshotImage) writeToFile:filePath atomically:YES]; 
//end guru 

UIImageWriteToSavedPhotosAlbum(screenshotImage, nil, nil, nil); 

我怎樣才能解決這個問題?當我使用Xcode4_iOS6構建時效果很好,但不適用於Xcode5_iOS7。

回答

2

您可以用這種方法解決它:

if([self IS_IOS_7]) 
    { 
     UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, TRUE, [[UIScreen mainScreen] scale]); 
    } 
    else 
    { 
     UIGraphicsBeginImageContext(self.view.bounds.size); 
    } 


    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; 

    UIImage *screenshotImage = UIGraphicsGetImageFromCurrentImageContext(); 

    UIGraphicsEndImageContext(); 

    UIImageWriteToSavedPhotosAlbum(screenshotImage, nil, nil, nil); 

iOS7檢查:

-(bool)IS_IOS_7 
{ 
    NSString *currOsVersion = [[UIDevice currentDevice] systemVersion]; 

    float sysver = [currOsVersion floatValue] ; 

    if(sysver >=7.0f) 
     return true; 

    return false; 
} 
+0

其工作。謝謝 – Guru

+1

IS_IOS_7真的有必要嗎?您可以使用if(UIGraphicsBeginImageContextWithOptions!= NULL)在所有支持它的操作系統版本上利用該方法。 –

+0

是的,這是有效的。 – iPhoneProcessor