2012-07-07 4 views
5
[[[globalSingleton paintingView] drawingView] setOpaque:NO]; 
[[[[globalSingleton paintingView] drawingView] layer] setOpaque:NO];  
[[[globalSingleton paintingView] drawingView] setBackgroundColor:[UIColor clearColor]]; 
[[[[globalSingleton paintingView] drawingView] layer] setBackgroundColor:[UIColor clearColor].CGColor]; 

[[[[globalSingleton paintingView] drawingView] layer] renderInContext:ctx]; 

UIImage *image1 = UIGraphicsGetImageFromCurrentImageContext(); 

위 코드는 'drawingView'를 png 파일에 저장하는 데 사용하는 코드입니다. 몇 가지 질문과 답변을 찾았습니다. 내 'drawingView'와 drawingView.layer의 불투명도를 'NO'로 설정하고 'drawingView'의 배경색을 [UIColor clearColor]로 설정합니다. 나는 stackoverflow에서 모든 대답을 적용한 것 같아요. 그러나 변경된 사항은 없습니다. PNG 파일의 배경은 여전히 ​​검은 색입니다. 검정이 아닌 투명한 배경이 필요합니다 !!투명 배경이있는 png 파일로 UIView를 저장하십시오.

UIImage * image1에 문제가 있는지 여부를 시도했습니다. 나는 image1을 사용하여 화면에 표시 한 다음 해당 이미지에서 검은 배경을 찾을 수있었습니다 1. 그래서 image1을 만들 때 문제가 있다는 것을 짐작할 수 있습니다.

이것은 내가 발견 한 것입니다. 투명 배경 이미지로 내 PNG 이미지를 저장할 수있는 가능한 해결책이 있습니까? 미리 감사드립니다 !!

답변

8

오, 세상에! 나는 그것을했다!! [[UIColor clearColor] set]을 추가했습니다. . 그게 다야.

UIGraphicsBeginImageContextWithOptions(screenRect.size, NO, [[UIScreen mainScreen] scale]); 

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

[[[globalSingleton paintingView] drawingView] setOpaque:NO]; 
[[[[globalSingleton paintingView] drawingView] layer] setOpaque:NO];  
[[[globalSingleton paintingView] drawingView] setBackgroundColor:[UIColor clearColor]]; 
[[[[globalSingleton paintingView] drawingView] layer] setBackgroundColor:[UIColor clearColor].CGColor]; 

[[[[globalSingleton paintingView] drawingView] layer] renderInContext:ctx]; 

UIImage *image1 = UIGraphicsGetImageFromCurrentImageContext(); 
관련 문제