diff --git a/packages/core/image-source/index.ios.ts b/packages/core/image-source/index.ios.ts index b47414a78d..b6d12f24d7 100644 --- a/packages/core/image-source/index.ios.ts +++ b/packages/core/image-source/index.ios.ts @@ -238,12 +238,10 @@ export class ImageSource implements ImageSourceDefinition { } const attributedString = NSAttributedString.alloc().initWithStringAttributes(source, >attributes); - - UIGraphicsBeginImageContextWithOptions(attributedString.size(), false, 0.0); - attributedString.drawAtPoint(CGPointMake(0, 0)); - - const iconImage = UIGraphicsGetImageFromCurrentImageContext(); - UIGraphicsEndImageContext(); + const renderer = UIGraphicsImageRenderer.alloc().initWithSize(attributedString.size()); + const iconImage = renderer.imageWithActions((context: UIGraphicsRendererContext) => { + attributedString.drawAtPoint(CGPointMake(0, 0)); + }); return iconImage ? new ImageSource(iconImage) : null; } @@ -443,18 +441,7 @@ export class ImageSource implements ImageSourceDefinition { } public resize(maxSize: number, options?: any): ImageSource { - const size: CGSize = this.ios.size; - const dim = getScaledDimensions(size.width, size.height, maxSize); - - const newSize: CGSize = CGSizeMake(dim.width, dim.height); - - UIGraphicsBeginImageContextWithOptions(newSize, options?.opaque ?? false, this.ios.scale); - this.ios.drawInRect(CGRectMake(0, 0, newSize.width, newSize.height)); - - const resizedImage = UIGraphicsGetImageFromCurrentImageContext(); - UIGraphicsEndImageContext(); - - return new ImageSource(resizedImage); + return new ImageSource(_resizeImage(this.ios, maxSize, options)); } public resizeAsync(maxSize: number, options?: any): Promise { @@ -465,16 +452,8 @@ export class ImageSource implements ImageSourceDefinition { const main_queue = dispatch_get_current_queue(); const background_queue = dispatch_get_global_queue(qos_class_t.QOS_CLASS_DEFAULT, 0); dispatch_async(background_queue, () => { - const size: CGSize = this.ios.size; - const dim = getScaledDimensions(size.width, size.height, maxSize); + const resizedImage = _resizeImage(this.ios, maxSize, options); - const newSize: CGSize = CGSizeMake(dim.width, dim.height); - - UIGraphicsBeginImageContextWithOptions(newSize, options?.opaque ?? false, this.ios.scale); - this.ios.drawInRect(CGRectMake(0, 0, newSize.width, newSize.height)); - - const resizedImage = UIGraphicsGetImageFromCurrentImageContext(); - UIGraphicsEndImageContext(); dispatch_async(main_queue, () => { resolve(new ImageSource(resizedImage)); }); @@ -483,6 +462,23 @@ export class ImageSource implements ImageSourceDefinition { } } +function _resizeImage(image: UIImage, maxSize: number, options?: any): UIImage { + const size: CGSize = image.size; + const dim = getScaledDimensions(size.width, size.height, maxSize); + const newSize: CGSize = CGSizeMake(dim.width, dim.height); + + const rendererFormat = UIGraphicsImageRendererFormat.defaultFormat(); + rendererFormat.opaque = !!options.opaque; + rendererFormat.scale = image.scale; + + const renderer = UIGraphicsImageRenderer.alloc().initWithSizeFormat(newSize, rendererFormat); + const resizedImage = renderer.imageWithActions((context: UIGraphicsRendererContext) => { + image.drawInRect(CGRectMake(0, 0, newSize.width, newSize.height)); + }); + + return resizedImage; +} + function getFileName(path: string): string { let fileName = typeof path === 'string' ? path.trim() : ''; if (fileName.indexOf('~/') === 0) { diff --git a/packages/core/platforms/ios/src/NativeScriptUtils.m b/packages/core/platforms/ios/src/NativeScriptUtils.m index ace5af1165..d8302a8b85 100644 --- a/packages/core/platforms/ios/src/NativeScriptUtils.m +++ b/packages/core/platforms/ios/src/NativeScriptUtils.m @@ -114,10 +114,15 @@ +(NSMutableAttributedString*)createMutableStringForSpan:(NSString*)text font:(UI +(UIImage*)scaleImage:(UIImage*)image width:(CGFloat)width height:(CGFloat)height scaleFactor:(CGFloat)scaleFactor { UIImage *resultImage; @autoreleasepool { - UIGraphicsBeginImageContextWithOptions(CGSizeMake(width, height), NO, scaleFactor); - [image drawInRect:CGRectMake(0, 0, width, height)]; - resultImage = UIGraphicsGetImageFromCurrentImageContext(); - UIGraphicsEndImageContext(); + UIGraphicsImageRendererFormat *format = [UIGraphicsImageRendererFormat defaultFormat]; + format.scale = scaleFactor; + format.opaque = NO; + + UIGraphicsImageRenderer *renderer = [[UIGraphicsImageRenderer alloc] initWithSize:CGSizeMake(width, height) format:format]; + + resultImage = [renderer imageWithActions:^(UIGraphicsImageRendererContext * _Nonnull context) { + [image drawInRect:CGRectMake(0, 0, width, height)]; + }]; } return resultImage; } diff --git a/packages/core/ui/styling/background.ios.ts b/packages/core/ui/styling/background.ios.ts index 8aa34e0c74..6024e7bfbd 100644 --- a/packages/core/ui/styling/background.ios.ts +++ b/packages/core/ui/styling/background.ios.ts @@ -627,59 +627,60 @@ function uiColorFromImage(img: UIImage, view: View, callback: (uiColor: UIColor) if (params.sizeX > 0 && params.sizeY > 0) { const resizeRect = CGRectMake(0, 0, params.sizeX, params.sizeY); - UIGraphicsBeginImageContextWithOptions(resizeRect.size, false, 0.0); - img.drawInRect(resizeRect); - img = UIGraphicsGetImageFromCurrentImageContext(); - UIGraphicsEndImageContext(); + const oldImage = img; + const renderer = UIGraphicsImageRenderer.alloc().initWithSize(resizeRect.size); + img = renderer.imageWithActions((context: UIGraphicsRendererContext) => { + oldImage.drawInRect(resizeRect); + }); } - UIGraphicsBeginImageContextWithOptions(CGSizeFromString(`{${boundsWidth},${boundsHeight}}`), false, 0.0); - const context = UIGraphicsGetCurrentContext(); + const renderer = UIGraphicsImageRenderer.alloc().initWithSize(CGSizeFromString(`{${boundsWidth},${boundsHeight}}`)); + const bgImage = renderer.imageWithActions((context: UIGraphicsRendererContext) => { + const ctx = context.CGContext; - if (background.color && background.color.ios) { - CGContextSetFillColorWithColor(context, background.color.ios.CGColor); - CGContextFillRect(context, CGRectMake(0, 0, boundsWidth, boundsHeight)); - } - - if (!params.repeatX && !params.repeatY) { - img.drawAtPoint(CGPointMake(params.posX, params.posY)); - } else { - const w = params.repeatX ? boundsWidth : img.size.width; - const h = params.repeatY ? boundsHeight : img.size.height; + if (background.color && background.color.ios) { + CGContextSetFillColorWithColor(ctx, background.color.ios.CGColor); + CGContextFillRect(ctx, CGRectMake(0, 0, boundsWidth, boundsHeight)); + } - CGContextSetPatternPhase(context, CGSizeMake(params.posX, params.posY)); + if (!params.repeatX && !params.repeatY) { + img.drawAtPoint(CGPointMake(params.posX, params.posY)); + } else { + const w = params.repeatX ? boundsWidth : img.size.width; + const h = params.repeatY ? boundsHeight : img.size.height; - params.posX = params.repeatX ? 0 : params.posX; - params.posY = params.repeatY ? 0 : params.posY; + CGContextSetPatternPhase(ctx, CGSizeMake(params.posX, params.posY)); - const patternRect = CGRectMake(params.posX, params.posY, w, h); + params.posX = params.repeatX ? 0 : params.posX; + params.posY = params.repeatY ? 0 : params.posY; - img.drawAsPatternInRect(patternRect); - } + const patternRect = CGRectMake(params.posX, params.posY, w, h); - const bkgImage = UIGraphicsGetImageFromCurrentImageContext(); - UIGraphicsEndImageContext(); + img.drawAsPatternInRect(patternRect); + } + }); if (flip) { - const flippedImage = _flipImage(bkgImage); + const flippedImage = _flipImage(bgImage); callback(UIColor.alloc().initWithPatternImage(flippedImage)); } else { - callback(UIColor.alloc().initWithPatternImage(bkgImage)); + callback(UIColor.alloc().initWithPatternImage(bgImage)); } } // Flipping the default coordinate system // https://developer.apple.com/library/ios/documentation/2DDrawing/Conceptual/DrawingPrintingiOS/GraphicsDrawingOverview/GraphicsDrawingOverview.html function _flipImage(originalImage: UIImage): UIImage { - UIGraphicsBeginImageContextWithOptions(originalImage.size, false, 0.0); - const context = UIGraphicsGetCurrentContext(); - CGContextSaveGState(context); - CGContextTranslateCTM(context, 0.0, originalImage.size.height); - CGContextScaleCTM(context, 1.0, -1.0); - originalImage.drawInRect(CGRectMake(0, 0, originalImage.size.width, originalImage.size.height)); - CGContextRestoreGState(context); - const flippedImage = UIGraphicsGetImageFromCurrentImageContext(); - UIGraphicsEndImageContext(); + const renderer = UIGraphicsImageRenderer.alloc().initWithSize(originalImage.size); + const flippedImage = renderer.imageWithActions((context: UIGraphicsRendererContext) => { + const ctx = context.CGContext; + + CGContextSaveGState(ctx); + CGContextTranslateCTM(ctx, 0.0, originalImage.size.height); + CGContextScaleCTM(ctx, 1.0, -1.0); + originalImage.drawInRect(CGRectMake(0, 0, originalImage.size.width, originalImage.size.height)); + CGContextRestoreGState(ctx); + }); return flippedImage; } diff --git a/packages/core/utils/ios/index.ts b/packages/core/utils/ios/index.ts index 6a28495809..eea4dd7cc0 100644 --- a/packages/core/utils/ios/index.ts +++ b/packages/core/utils/ios/index.ts @@ -215,10 +215,17 @@ export function snapshotView(view: UIView, scale: number): UIImage { // console.log('snapshotView view.frame:', printRect(view.frame)); const originalOpacity = view.layer.opacity; view.layer.opacity = originalOpacity > 0 ? originalOpacity : 1; - UIGraphicsBeginImageContextWithOptions(CGSizeMake(view.frame.size.width, view.frame.size.height), false, scale); - view.layer.renderInContext(UIGraphicsGetCurrentContext()); - const image = UIGraphicsGetImageFromCurrentImageContext(); - UIGraphicsEndImageContext(); + + const size = CGSizeMake(view.frame.size.width, view.frame.size.height); + const rendererFormat = UIGraphicsImageRendererFormat.defaultFormat(); + rendererFormat.scale = scale; + + const renderer = UIGraphicsImageRenderer.alloc().initWithSizeFormat(size, rendererFormat); + const image = renderer.imageWithActions((context: UIGraphicsRendererContext) => { + const ctx = context.CGContext; + view.layer.renderInContext(ctx); + }); + setTimeout(() => { // ensure set back properly on next tick view.layer.opacity = originalOpacity;