Is there any working sample for iOS?

3 views
Skip to first unread message

Candy Man

unread,
Sep 4, 2025, 10:30:39 AM (3 days ago) Sep 4
to skia-discuss
I created one. But it always crash when I draw with Skia. Here is my code:

#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,

Reply all
Reply to author
Forward
0 new messages