2012-05-25 154 views
6

我正在iphone上工作,我把UIView的子類和繪製矩形方法做了一些設計。我想以.png格式轉換這個視圖。將uiview轉換爲.png圖像

在此先感謝。

+0

ü要獲取的UIView的屏幕截圖? –

+0

你想要圖像轉換成.png格式或屏幕捕獲 –

回答

21
UIGraphicsBeginImageContext(myView.frame.size); 
    [myView.layer renderInContext:UIGraphicsGetCurrentContext()]; 
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    NSData *data=UIImagePNGRepresentation(viewImage); 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *strPath = [documentsDirectory stringByAppendingPathComponent:@"myimage.png"]; 
    [data writeToFile:strPath atomically:YES]; 
+0

感謝它顯示圖像在控制檯中,但是當我在imageview上顯示它不顯示幫助我 – Chandni

+0

檢查您的IBOutlet imageview是否設置 – Dhawal

0
UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow]; 
    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) 
     UIGraphicsBeginImageContextWithOptions(YourView.frame.size, NO, [UIScreen mainScreen].scale); 
    else 
     UIGraphicsBeginImageContext(keyWindow.bounds.size); 

    CGContextRef context = UIGraphicsGetCurrentContext(); 
    [keyWindow.layer renderInContext:context]; 
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext(); 
    UIImageWriteToSavedPhotosAlbum(img, nil, nil, nil); 
    UIGraphicsEndImageContext();