简体   繁体   English

将UIView保存到透明的PNG中

[英]Save UIView to a transparent PNG

I have a UIView and I want it to be stored as a transparent PNG, ie without the UIVIew background color... 我有一个UIView,我希望它存储为透明的PNG,即没有UIVIew背景颜色......

I am currently using this code and it's working OK but with the background color :( 我目前正在使用此代码,它工作正常,但背景颜色:(

UIGraphicsBeginImageContext(self.bounds.size);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage* image1 = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *imageData = UIImagePNGRepresentation(image1);
[imageData writeToFile:filePath atomically:YES];

So does anyone have any idea about getting this image as a transparent one? 那么有没有人知道如何将这个图像作为一个透明的图像?

Thank you in advance.. 先感谢您..

In my case, I forgot the opaque property. 就我而言,我忘记了不透明的财产。 It should be set to NO: 它应该设置为NO:

view.opaque = NO;

Change the background color to [UIColor clear], draw the image and then set the background color back to the original color. 将背景颜色更改为[UIColor clear],绘制图像,然后将背景颜色设置回原始颜色。

Since GUI updates will fire only on the next runloop cycle, the user should not see any flickering. 由于GUI更新仅在下一个runloop循环中触发,因此用户不应该看到任何闪烁。

UIColor* color=self.backgroundColor;
view.backgroundColor=[UIColor clearColor];

UIGraphicsBeginImageContext(self.bounds.size);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage* image1 = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *imageData = UIImagePNGRepresentation(image1);
[imageData writeToFile:filePath atomically:YES];

self.backgroundColor=color;

As an addition to Gilad's answer. 作为吉拉德答案的补充。 On retina display this may cause some quality issues. 在视网膜显示器上,这可能会导致一些质量问题。 To get the retina context you may use this code from this post. 要获取视网膜上下文,您可以使用帖子中的此代码。

UIColor* color=self.backgroundColor;
view.backgroundColor=[UIColor clearColor];

// This is for retina render check
UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
    UIGraphicsBeginImageContextWithOptions(YourView.frame.size, NO, [UIScreen mainScreen].scale);
else
    UIGraphicsBeginImageContext(keyWindow.bounds.size);
// And it goes up to here the rest stays the same

[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage* image1 = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *imageData = UIImagePNGRepresentation(image1);
[imageData writeToFile:filePath atomically:YES];

self.backgroundColor=color;

For Objective-c you have to set opaque = NO 对于Objective-c,您必须设置opaque = NO

 + (UIImage *) imageWithView:(UIView *)view 
    {     
        UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, [[UIScreen mainScreen] scale]);     
        [view.layer renderInContext:UIGraphicsGetCurrentContext()];     
        UIImage * img = UIGraphicsGetImageFromCurrentImageContext();     
        UIGraphicsEndImageContext();     
        return img; 
    }

For Swift you have to set opaque = false 对于Swift,您必须设置opaque = false

func imageWithView(inView: UIView) -> UIImage? {
        UIGraphicsBeginImageContextWithOptions(inView.bounds.size, false, 0.0)
        if let context = UIGraphicsGetCurrentContext() {
            inView.layer.render(in: context)
            let image = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            return image
        }
        return nil
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM