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

App craches, the package bluetooth_print can't print images on ios, it's working in android but doesn't work in iOS #178

Open
HamzaMihfad opened this issue Oct 30, 2023 · 6 comments

Comments

@HamzaMihfad
Copy link

Here is the error message in xCode:

Exception NSException * "UIGraphicsBeginImageContext() failed to allocate CGBitampContext: size={0, 0}, scale=1.000000, bitmapInfo=0x2002. Use UIGraphicsImageRenderer to avoid this assert." 0x00000002837dae50

@aargjjendi
Copy link

I'm having the same issue, did you find any solution ?

@Scuttman
Copy link

Scuttman commented Feb 9, 2024

I was having the same issue in Android. I noticed that the example app on pub.dev had the line to show the image commented out!

However, I discovered that the error is because the image size has not been set. For the image, you have to set an int value under width:200 for example. This then works. Have not tested in IOS but not setting the width caused a compile error. Once the width was set, error was removed and image printed.

printList.add(LineText(type: LineText.TYPE_IMAGE, width:200, content: base64Image, align: LineText.ALIGN_CENTER, linefeed: 1));

@Niagawan-mave
Copy link

I am also facing the same issues, did anyone have the solution ?
I have set the width and height to 100. it work great in android, but in IOS it cannot print image, it show the error above

@Niagawan-mave
Copy link

I have solved the problem but adding resizing code in IOS
You can go to ios/Classes/BluetoothPrintPlugin.m
Then look for function mapToEscCommand, within it have a part is else if([@"image" isEqualToString:type]){
replace the code below will solved the ios printer image cannot print problem

else if([@"image" isEqualToString:type]){
NSData *decodeData = [[NSData alloc] initWithBase64EncodedString:content options:0];
UIImage *image = [UIImage imageWithData:decodeData];
// Define the maximum width
CGFloat maxWidth = 100.0; // Adjust the maximum width as needed

        // Calculate the target size while maintaining the original aspect ratio
        CGSize originalSize = image.size;
        CGFloat scaleFactor = maxWidth / originalSize.width;
        CGSize targetSize = CGSizeMake(originalSize.width * scaleFactor, originalSize.height * scaleFactor);

        // Create a renderer with the calculated target size
        UIGraphicsImageRenderer *renderer = [[UIGraphicsImageRenderer alloc] initWithSize:targetSize];

        // Render the image and get a new data representation
        NSData *renderedImageData = [renderer JPEGDataWithCompressionQuality:1 actions:^(UIGraphicsImageRendererContext * _Nonnull context) {
            [image drawInRect:CGRectMake(0, 0, targetSize.width, targetSize.height)];
        }];
        UIImage *resizedImage = [UIImage imageWithData:renderedImageData];
        [command addOriginrastBitImage:resizedImage];
    }

@HamzaMihfad
Copy link
Author

@Niagawan-mave thanks a lot, finally it's working on ios now

did you create a new package on pub.dev? or just forked it to your github account?

@Niagawan-mave
Copy link

@HamzaMihfad I just forked to my github account

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

4 participants