Skip to content

Commit 3f43524

Browse files
committed
fix(ios): prevent crash when printing images without internet connection
- Add validation checks in ImageManager.m to handle nil data and invalid image dimensions - Add error handling in ThePrinter.m to return EPOS2_ERR_PARAM instead of crashing - Prevent UIGraphicsBeginImageContext crash with invalid size parameters - Fix division by zero in getImageCGSize when image width is 0 Fixes crash: UIGraphicsBeginImageContext() failed to allocate CGBitampContext: size={472, 0}
1 parent 96109b6 commit 3f43524

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

package/ios/ImageManager.m

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ @implementation ImageManager: NSObject
66
+ (UIImage *)scaleImage:(UIImage *)image
77
size:(CGSize)size
88
{
9+
if (!image || image.size.width <= 0 || image.size.height <= 0 || size.width <= 0 || size.height <= 0) {
10+
return nil;
11+
}
12+
913
CGFloat scale = MAX(size.width/image.size.width, size.height/image.size.height);
1014
CGFloat width = image.size.width * scale;
1115
CGFloat height = image.size.height * scale;
@@ -25,6 +29,10 @@ + (UIImage *)scaleImage:(UIImage *)image
2529
+ (CGSize)getImageCGSize:(UIImage *)imageData
2630
width:(int)width
2731
{
32+
if (!imageData || imageData.size.width <= 0 || imageData.size.height <= 0 || width <= 0) {
33+
return CGSizeZero;
34+
}
35+
2836
NSInteger imgHeight = imageData.size.height;
2937
NSInteger imagWidth = imageData.size.width;
3038

@@ -38,7 +46,11 @@ + (UIImage *)getImageFromDictionarySource:(NSDictionary *)imageObj
3846
if([urlString hasPrefix: @"http"] || [urlString hasPrefix: @"https"]) {
3947
NSURL *url = [NSURL URLWithString: urlString];
4048
NSData *data = [NSData dataWithContentsOfURL:url];
41-
imageData = [[UIImage alloc] initWithData:data];
49+
if (data && data.length > 0) {
50+
imageData = [[UIImage alloc] initWithData:data];
51+
} else {
52+
imageData = nil;
53+
}
4254
} else {
4355
imageData = [RCTConvert UIImage:imageObj];
4456
}

package/ios/ThePrinter.m

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,8 +387,22 @@ -(int) addImage: (NSDictionary *)source
387387
return EPOS2_ERR_MEMORY;
388388
}
389389
UIImage *data = [ImageManager getImageFromDictionarySource:source];
390+
// Check if image loading failed (e.g., due to network issues)
391+
if (data == nil) {
392+
return EPOS2_ERR_PARAM;
393+
}
394+
390395
CGSize size = [ImageManager getImageCGSize:data width:width];
396+
// Check if size calculation failed (e.g., invalid image dimensions)
397+
if (size.width <= 0 || size.height <= 0) {
398+
return EPOS2_ERR_PARAM;
399+
}
400+
391401
UIImage *scaledImage = [ImageManager scaleImage:data size:size];
402+
// Check if image scaling failed
403+
if (scaledImage == nil) {
404+
return EPOS2_ERR_PARAM;
405+
}
392406

393407
int result = [epos2Printer_ addImage: scaledImage x:0 y:0 width:size.width height:size.height color:color mode:mode halftone:halftone brightness:brightness compress:compress];
394408
return result;

0 commit comments

Comments
 (0)