Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

ref(ios): Update deprecated UIGraphics API #10604

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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
Loading
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 23 additions & 27 deletions 50 packages/core/image-source/index.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,12 +238,10 @@ export class ImageSource implements ImageSourceDefinition {
}

const attributedString = NSAttributedString.alloc().initWithStringAttributes(source, <NSDictionary<string, any>>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;
}
Expand Down Expand Up @@ -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<ImageSource> {
Expand All @@ -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));
});
Expand All @@ -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) {
Expand Down
13 changes: 9 additions & 4 deletions 13 packages/core/platforms/ios/src/NativeScriptUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
71 changes: 36 additions & 35 deletions 71 packages/core/ui/styling/background.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
15 changes: 11 additions & 4 deletions 15 packages/core/utils/ios/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.