2012-07-07 41 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(); 

Đoạn mã trên là những gì tôi đang sử dụng để lưu 'drawingView' vào tệp png. Tôi đã tìm thấy một số câu hỏi và câu trả lời, vì vậy tôi đã áp dụng chúng. Tôi đặt opaque của 'drawingView' và drawingView.layer thành NO, và thiết lập màu nền của 'drawingView' là [UIColor clearColor]. Tôi nghĩ rằng tôi áp dụng tất cả các câu trả lời từ stackoverflow. Tuy nhiên, không có gì thay đổi. Nền của png fil vẫn là màu đen. Tôi cần nền trong suốt, không phải màu đen !!lưu UIView dưới dạng tệp png với nền trong suốt

Tôi đã thử xem có vấn đề gì với UIImage * image1 hay không. Tôi đã sử dụng image1 để hiển thị trên màn hình, sau đó tôi có thể tìm thấy nền đen từ hình ảnh đó1. Vì vậy, tôi có thể đoán có bất kỳ vấn đề nào khi tạo image1.

Đây là tất cả những gì tôi tìm thấy. Có giải pháp nào có thể để lưu hình ảnh png của tôi với hình nền trong suốt không? Cảm ơn trước!!

Trả lời

8

Ôi trời! Tôi đã làm nó!! Tôi đã thêm [[UIColor clearColor] set]; . Đó là tất cả.

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(); 
Các vấn đề liên quan