Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enabling Nativescript-screenshot for {N} 7/8 #15

Open
dlcole opened this issue Jan 8, 2022 · 0 comments
Open

Enabling Nativescript-screenshot for {N} 7/8 #15

dlcole opened this issue Jan 8, 2022 · 0 comments

Comments

@dlcole
Copy link

dlcole commented Jan 8, 2022

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch nativescript-screenshot@0.0.2 for the project I'm working on.

I revised the package to be compatible with the breaking changes in NativeScript 7 and 8: changing the import statement, replacing "exports." with "export", and modifying the ImageSource instantiation on iOS. It turns out ImageSource.fromData now returns a promise, so I modified the Android code to likewise return a promise. This means anyone invoking screenshot.getImage must now expect a promise, but their code would be broken on iOS anyway if they didn't.

Here is the diff that solved my problem:

diff --git a/node_modules/nativescript-screenshot/index.js b/node_modules/nativescript-screenshot/index.js
index 8be9bef..6c67b19 100644
--- a/node_modules/nativescript-screenshot/index.js
+++ b/node_modules/nativescript-screenshot/index.js
@@ -1,20 +1,20 @@
-var imageSource = require("image-source");
+import { ImageSource } from '@nativescript/core';
 
-exports.getImage = function(view) {
+export function getImage(view) {
     if (view.ios) {
         UIGraphicsBeginImageContextWithOptions(view.ios.frame.size, false, 0);
         view.ios.drawViewHierarchyInRectAfterScreenUpdates(CGRectMake(0, 0, view.ios.frame.size.width, view.ios.frame.size.height), true);
         var imageFromCurrentImageContext = UIGraphicsGetImageFromCurrentImageContext();
         UIGraphicsEndImageContext();
-        return imageSource.fromData(UIImagePNGRepresentation(imageFromCurrentImageContext));
+        return ImageSource.fromData(UIImagePNGRepresentation(imageFromCurrentImageContext)); // returns Promise
         
     } else if (view.android) {
         view.android.setDrawingCacheEnabled(true);
         var bmp = android.graphics.Bitmap.createBitmap(view.android.getDrawingCache());
         view.android.setDrawingCacheEnabled(false);
-        var source = new imageSource.ImageSource();
+        var source = new ImageSource;
         source.setNativeSource(bmp);
-        return source;
+        return Promise.resolve(source);
     }
     return undefined;
 }
\ No newline at end of file

This issue body was partially generated by patch-package.

@dlcole dlcole mentioned this issue Jan 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant