bitblit https://www.e-learn.cn/tag/bitblit zh-hans How to render to offscreen bitmap then blit to screen using Core Graphics https://www.e-learn.cn/topic/570218 <span>How to render to offscreen bitmap then blit to screen using Core Graphics</span> <span><span lang="" about="/user/29" typeof="schema:Person" property="schema:name" datatype="">穿精又带淫゛_</span></span> <span>2019-11-29 01:22:20</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><div class="alert alert-danger" role="alert"> <p>I would like to render to an offscreen bitmap (or array of RGBA values) and then blit those to a <code>UIView</code> during in the view's <code>drawRect</code> function. I would prefer to do full 32-bit rendering (including alpha channel), but would also be content with 24-bit rendering.</p> <p>Would anyone mind pointing me in the right direction with some code snippets or relevant APIs?</p> <p>Also, I know exactly how to do this using OpenGL - I would just prefer to do this work in Core Graphics itself.</p> </div><div class="panel panel-info"><div class="panel-heading"></div><div class="panel-body"> <p>To render into an offscreen context and save it as a CGImageRef:</p> <pre><code>void *bitmapData = calloc(height, bytesPerLine); CGContextRef offscreen = CGBitmapContextCreate(..., bitmapData, ...) // draw stuff into offscreen CGImageRef image = CGBitmapContextCreateImage(offscreen); CFRelease(offscreen); free(bitmapData); </code></pre> <p>To draw it on the screen:</p> <pre><code>- (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext(); CGContextDrawImage(context, rect, image); } </code></pre> <p>You could also just save the image in the view's layer's contents property (<code>view.layer.contents = image</code>), or use a UIImageView.</p> </div></div><div class="panel panel-info"><div class="panel-heading"></div><div class="panel-body"> <p>You can use a <a href="http://developer.apple.com/documentation/GraphicsImaging/Reference/CGBitmapContext/Reference/reference.html" rel="nofollow">CGBitmapContext</a>. You can generate an image from a CGBitmapContext and draw it during your drawRect.</p> </div></div><div class="panel panel-info"><div class="panel-heading"></div><div class="panel-body"> <p>Use <code>CGDataProviderCreateWithData</code> and <code>CGImageCreate</code> if you don't need the bitmap context and just want the <code>CGImageRef</code>.</p> </div></div><div class="panel panel-info"><div class="panel-heading"></div><div class="panel-body"> <p>For future reference, here is a full example in Swift 2.1 of rendering to an offscreen bitmap and displaying it on the screen. </p> <p>Note that once you have created the bitmap context you can keep drawing more content into it, and updating the view when you want. This is great if you want to do a lengthy drawing operation on a background thread and periodically show progress to the user.</p> <p><strong>View Controller:</strong></p> <pre><code>import UIKit class ViewController: UIViewController { @IBOutlet var myView: MyView! var bitmapContext: CGContext? override func viewDidLoad() { super.viewDidLoad() createBitmapContext() drawContentIntoBitmap() myView.update(from: bitmapContext) releaseBitmapContext() } func createBitmapContext() { bitmapContext = CGBitmapContextCreate( nil, // auto-assign memory for the bitmap Int (myView.bounds.width * UIScreen.mainScreen().scale), // width of the view in pixels Int (myView.bounds.height * UIScreen.mainScreen().scale), // height of the view in pixels 8, // 8 bits per colour component 0, // auto-calculate bytes per row CGColorSpaceCreateDeviceRGB(), // create a suitable colour space CGImageAlphaInfo.PremultipliedFirst.rawValue) // use quartz-friendly byte ordering } func drawContentIntoBitmap() { CGContextScaleCTM(bitmapContext, UIScreen.mainScreen().scale, UIScreen.mainScreen().scale) // convert to points dimensions CGContextSetStrokeColorWithColor (bitmapContext, UIColor.redColor().CGColor) CGContextSetLineWidth (bitmapContext, 5.0) CGContextStrokeEllipseInRect (bitmapContext, CGRectMake(50, 50, 100, 100)) } func releaseBitmapContext() { bitmapContext = nil // in Swift, CGContext and CGColorSpace objects are memory managed by automatic reference counting } } </code></pre> <p><strong>Subclass of UIView:</strong></p> <pre><code>import UIKit class MyView: UIView { var cgImage: CGImage? func update(from bitmapContext: CGContext?) { cgImage = CGBitmapContextCreateImage(bitmapContext) setNeedsDisplay() } override func drawRect(rect: CGRect) { let displayContext = UIGraphicsGetCurrentContext() CGContextDrawImage(displayContext, bounds, cgImage) } } </code></pre> </div></div><div class="alert alert-warning" role="alert"><p>来源:<code>https://stackoverflow.com/questions/410313/how-to-render-to-offscreen-bitmap-then-blit-to-screen-using-core-graphics</code></p></div></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/iphone" hreflang="zh-hans">iphone</a></div> <div class="field--item"><a href="/tag/bitmap" hreflang="zh-hans">bitmap</a></div> <div class="field--item"><a href="/tag/core-graphics" hreflang="zh-hans">core-graphics</a></div> <div class="field--item"><a href="/tag/bitblit" hreflang="zh-hans">bitblit</a></div> </div> </div> Thu, 28 Nov 2019 17:22:20 +0000 穿精又带淫゛_ 570218 at https://www.e-learn.cn How to render to offscreen bitmap then blit to screen using Core Graphics https://www.e-learn.cn/topic/342889 <span>How to render to offscreen bitmap then blit to screen using Core Graphics</span> <span><span lang="" about="/user/234" typeof="schema:Person" property="schema:name" datatype="">喜你入骨</span></span> <span>2019-11-27 15:49:12</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>I would like to render to an offscreen bitmap (or array of RGBA values) and then blit those to a <code>UIView</code> during in the view's <code>drawRect</code> function. I would prefer to do full 32-bit rendering (including alpha channel), but would also be content with 24-bit rendering.</p> <p>Would anyone mind pointing me in the right direction with some code snippets or relevant APIs?</p> <p>Also, I know exactly how to do this using OpenGL - I would just prefer to do this work in Core Graphics itself.</p> <br /><h3>回答1:</h3><br /><p>To render into an offscreen context and save it as a CGImageRef:</p> <pre><code>void *bitmapData = calloc(height, bytesPerLine); CGContextRef offscreen = CGBitmapContextCreate(..., bitmapData, ...) // draw stuff into offscreen CGImageRef image = CGBitmapContextCreateImage(offscreen); CFRelease(offscreen); free(bitmapData); </code></pre> <p>To draw it on the screen:</p> <pre><code>- (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext(); CGContextDrawImage(context, rect, image); } </code></pre> <p>You could also just save the image in the view's layer's contents property (<code>view.layer.contents = image</code>), or use a UIImageView.</p> <br /><br /><br /><h3>回答2:</h3><br /><p>You can use a CGBitmapContext. You can generate an image from a CGBitmapContext and draw it during your drawRect.</p> <br /><br /><br /><h3>回答3:</h3><br /><p>Use <code>CGDataProviderCreateWithData</code> and <code>CGImageCreate</code> if you don't need the bitmap context and just want the <code>CGImageRef</code>.</p> <br /><br /><br /><h3>回答4:</h3><br /><p>For future reference, here is a full example in Swift 2.1 of rendering to an offscreen bitmap and displaying it on the screen. </p> <p>Note that once you have created the bitmap context you can keep drawing more content into it, and updating the view when you want. This is great if you want to do a lengthy drawing operation on a background thread and periodically show progress to the user.</p> <p><strong>View Controller:</strong></p> <pre><code>import UIKit class ViewController: UIViewController { @IBOutlet var myView: MyView! var bitmapContext: CGContext? override func viewDidLoad() { super.viewDidLoad() createBitmapContext() drawContentIntoBitmap() myView.update(from: bitmapContext) releaseBitmapContext() } func createBitmapContext() { bitmapContext = CGBitmapContextCreate( nil, // auto-assign memory for the bitmap Int (myView.bounds.width * UIScreen.mainScreen().scale), // width of the view in pixels Int (myView.bounds.height * UIScreen.mainScreen().scale), // height of the view in pixels 8, // 8 bits per colour component 0, // auto-calculate bytes per row CGColorSpaceCreateDeviceRGB(), // create a suitable colour space CGImageAlphaInfo.PremultipliedFirst.rawValue) // use quartz-friendly byte ordering } func drawContentIntoBitmap() { CGContextScaleCTM(bitmapContext, UIScreen.mainScreen().scale, UIScreen.mainScreen().scale) // convert to points dimensions CGContextSetStrokeColorWithColor (bitmapContext, UIColor.redColor().CGColor) CGContextSetLineWidth (bitmapContext, 5.0) CGContextStrokeEllipseInRect (bitmapContext, CGRectMake(50, 50, 100, 100)) } func releaseBitmapContext() { bitmapContext = nil // in Swift, CGContext and CGColorSpace objects are memory managed by automatic reference counting } } </code></pre> <p><strong>Subclass of UIView:</strong></p> <pre><code>import UIKit class MyView: UIView { var cgImage: CGImage? func update(from bitmapContext: CGContext?) { cgImage = CGBitmapContextCreateImage(bitmapContext) setNeedsDisplay() } override func drawRect(rect: CGRect) { let displayContext = UIGraphicsGetCurrentContext() CGContextDrawImage(displayContext, bounds, cgImage) } } </code></pre> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/410313/how-to-render-to-offscreen-bitmap-then-blit-to-screen-using-core-graphics</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/iphone" hreflang="zh-hans">iphone</a></div> <div class="field--item"><a href="/tag/bitmap" hreflang="zh-hans">bitmap</a></div> <div class="field--item"><a href="/tag/core-graphics" hreflang="zh-hans">core-graphics</a></div> <div class="field--item"><a href="/tag/bitblit" hreflang="zh-hans">bitblit</a></div> </div> </div> Wed, 27 Nov 2019 07:49:12 +0000 喜你入骨 342889 at https://www.e-learn.cn