gpt4 book ai didi

objective-c - 如何用 UIImage 填充 CGContext?

转载 作者:行者123 更新时间:2023-11-29 11:11:12 25 4
gpt4 key购买 nike

我继承了UIView,我覆盖了-drawRect:,我通过CGContext画了一个图形。现在我想用 UIImage 填充这个图。知道怎么做吗?

编辑:

我的代码来自 -drawRect:

CGContextBeginPath(ctx);
//here I draw my figure
CGContextClosePath(ctx);
CGContextClip(ctx);

//this doesn't work
UIImage * image = [UIImage imageNamed:@"blackbackground.jgp"];
[image drawInRect:rect];

//this neither
CGImageRef cgImage = image.CGImage;
CGContextDrawImage(ctx, rect, cgImage);

最佳答案

将图像绘制到 CGContext 的正确方法是这样的:

UIImage *_originalImage = // your image

UIGraphicsBeginImageContext(_originalImage.size);
CGContextRef _context = UIGraphicsGetCurrentContext(); // here you don't need this reference for the context but if you want to use in the future for drawing anything else on the context you could get it for it
[_originalImage drawInRect:CGRectMake(0.f, 0.f, _originalImage.size.width, _originalImage.size.height)];

UIImage *_newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

关于objective-c - 如何用 UIImage 填充 CGContext?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11522170/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com