#ifdef __cplusplus
// Skia headers
#include "include/core/SkCanvas.h"
#include "include/core/SkColor.h"
#include "include/core/SkImage.h"
#include "include/core/SkPaint.h"
#include "include/core/SkSurface.h"
#endif
@interface SkiaDemoViewController () {
SkCanvas* _canvas;
}
@property (nonatomic, strong) CADisplayLink *displayLink;
@end
@implementation SkiaDemoViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor systemBackgroundColor];
self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(redraw)];
[self.displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];}
-(void)redraw {
if (_canvas == NULL) {
const CGFloat scale = UIScreen.mainScreen.scale;
const int width = (int)llround(UIScreen.mainScreen.bounds.size.width * scale);
const int height = (int)llround(UIScreen.mainScreen.bounds.size.height * scale);
// Step 1: Create a Skia surface
SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
sk_sp<SkSurface> surface = SkSurfaces::Raster(info);
_canvas = surface->getCanvas();
}
[self draw: _canvas];
}
-(void)draw:(SkCanvas *) canvas {
canvas->drawColor(SK_ColorWHITE);
SkPaint paint;
paint.setStyle(SkPaint::kFill_Style);
paint.setAntiAlias(true);
paint.setStrokeWidth(4);
paint.setColor(0xff4285F4);
SkRect rect = SkRect::MakeXYWH(10, 10, 100, 160);
canvas->drawRect(rect, paint);
}
@end
It crashes in my draw method. Any idea? Thanks,