简体   繁体   中英

objective-c - Create an UIImage with Core Graphics

I have this code and it's not adding anything to the view, can you help me figure out what is wrong? also I will apply a filer to image, but that will be later, when I get this done, but tell me if this is a wrong approach if Im going to add filters.

UIGraphicsPushContext(context); 

CALayer *layer = [CALayer layer];
CGColorRef bgColor = [UIColor colorWithHue:0.6 saturation:1.0 brightness:1.0 alpha:1.0].CGColor;
layer.frame = CGRectMake(0, 0, 200, 200);

CGContextSetFillColorWithColor(context, bgColor);
CGContextFillRect(context, layer.bounds);

CGContextSaveGState(context);
CGColorSpaceRef patternSpace = CGColorSpaceCreatePattern(NULL);
CGContextSetFillColorSpace(context, patternSpace);
CGColorSpaceRelease(patternSpace);

CGContextFillRect(context, layer.bounds);
CGContextRestoreGState(context);    
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

UIImageView *borrar = [[UIImageView alloc]initWithImage:image];

[self.view addSubview:borrar];

You never open an image context. Use UIGraphicsBeginImageContextWithOptions .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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