subharb
subharb

Reputation: 3472

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];

Upvotes: 0

Views: 422

Answers (1)

user187676
user187676

Reputation:

You never open an image context. Use UIGraphicsBeginImageContextWithOptions.

Upvotes: 0

Related Questions