Skip to content

Commit abddc8e

Browse files
authored
fix(ios): prevent crash when printing images without internet connection (#229)
* 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} * fix: review comments * fix: error code returned
1 parent 143b8d7 commit abddc8e

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

package/ios/ImageManager.m

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@ + (UIImage *)getImageFromDictionarySource:(NSDictionary *)imageObj
3838
if([urlString hasPrefix: @"http"] || [urlString hasPrefix: @"https"]) {
3939
NSURL *url = [NSURL URLWithString: urlString];
4040
NSData *data = [NSData dataWithContentsOfURL:url];
41-
imageData = [[UIImage alloc] initWithData:data];
41+
if (data && data.length > 0) {
42+
imageData = [[UIImage alloc] initWithData:data];
43+
} else {
44+
imageData = nil;
45+
}
4246
} else {
4347
imageData = [RCTConvert UIImage:imageObj];
4448
}

package/ios/ThePrinter.m

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,11 @@ -(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_FAILURE;
393+
}
394+
390395
CGSize size = [ImageManager getImageCGSize:data width:width];
391396
UIImage *scaledImage = [ImageManager scaleImage:data size:size];
392397

0 commit comments

Comments
 (0)