How to use setImage in Sketch Plugins

220 Views Asked by At

Im currently writing a Sketch Plugin.

I'm doning something like this:

fill.setFillType(4);
fill.setPatternImage( imageData );
fill.setPatternFillType(1);

The console Outputs: setPatternImage: is deprecated. Use setImage: instead with MSImageData «Scripting Legacy»

How can I solve this? This cause a crash.

fill.setImage( imageData )

Thanks in advance.

1

There are 1 best solutions below

2
On

Did you look at sketch-headers about new version? https://github.com/abynim/Sketch-Headers

you can change your codes like, if you'd like to build a plugin which supports all versions:

firstly you need to catch your sketch version as a variable.

function getSketchVersionNumber() {
    const version = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"]
    var versionNumber = version.stringByReplacingOccurrencesOfString_withString(".", "") + ""
    while(versionNumber.length != 3) {
        versionNumber += "0"
    }
    return parseInt(versionNumber)
}

var sketchVersion = getSketchVersionNumber()

fill.setFillType(4)

if (sketchVersion > 370) {
  // you need to grab the image with MSImageData like this one
  var image = [[MSImageData alloc] initWithImage: imageData convertColorSpace: false]]
  fill.setImage(image)

} else if(sketchVersion < 350) {
 [fill setPatternImage:imageData collection:[[fill documentData] images]]
} else {
 fill.setPatternImage(imageData)
}
fill.setPatternFillType(1)