كيفية تقديم صورة نقطية خارج الشاشة ثم قم ببراعة إلى الشاشة باستخدام الرسومات الأساسية
-
03-07-2019 - |
سؤال
أود أن أقدم إلى صورة نقطية خارج الشاشة (أو صفيف من قيم RGBA) ثم قم ببزيرة تلك إلى أ UIView
أثناء المنظر drawRect
وظيفة. أفضل أن أقوم بتقديم 32 بت كامل (بما في ذلك قناة ألفا) ، ولكن سيكون أيضًا راضيا عن عرض 24 بت.
هل يمانع أي شخص في توجيه لي في الاتجاه الصحيح مع بعض قصاصات الكود أو واجهات برمجة التطبيقات ذات الصلة؟
أيضًا ، أعرف بالضبط كيفية القيام بذلك باستخدام OpenGL - أفضل فقط القيام بهذا العمل في الرسومات الأساسية نفسها.
المحلول
لتقديم سياق خارج الشاشة وحفظه باعتباره cgimageref:
void *bitmapData = calloc(height, bytesPerLine);
CGContextRef offscreen = CGBitmapContextCreate(..., bitmapData, ...)
// draw stuff into offscreen
CGImageRef image = CGBitmapContextCreateImage(offscreen);
CFRelease(offscreen);
free(bitmapData);
لرسمها على الشاشة:
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextDrawImage(context, rect, image);
}
يمكنك أيضًا حفظ الصورة في خاصية محتويات طبقة العرض (view.layer.contents = image
) ، أو استخدم UiimageView.
نصائح أخرى
يمكنك استخدام أ cgbitmapcontext. يمكنك إنشاء صورة من cgbitmapcontext ورسمها أثناء القسط الخاص بك.
يستخدم CGDataProviderCreateWithData
و CGImageCreate
إذا لم تكن بحاجة إلى سياق صورة نقطية وتريد فقط CGImageRef
.
للرجوع إليها في المستقبل ، إليك مثال كامل في Swift 2.1 من تقديم صورة نقطية خارج الشاشة وعرضها على الشاشة.
لاحظ أنه بمجرد إنشاء سياق BITMAP ، يمكنك الاستمرار في رسم المزيد من المحتوى فيه ، وتحديث العرض عندما تريد. يعد هذا أمرًا رائعًا إذا كنت ترغب في القيام بعملية رسم مطولة على موضوع خلفية وإظهار التقدم بشكل دوري للمستخدم.
عرض وحدة التحكم:
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
}
}
الفئة الفرعية من Uiview:
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)
}
}