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

ios17点击查看图片崩溃 #356

Open
wydgithub opened this issue Mar 7, 2024 · 2 comments
Open

ios17点击查看图片崩溃 #356

wydgithub opened this issue Mar 7, 2024 · 2 comments

Comments

@wydgithub
Copy link

Description

调用方法
YBIBImageData *ybData = [YBIBImageData new];
ybData.imageURL = [NSURL URLWithString:urlString];
ybData.projectiveView = nil;
YBImageBrowser *browser = [YBImageBrowser new];
browser.dataSourceArray = @[ybData];
browser.currentPage = 0;
[browser show];

崩溃的地方
UIImage *YBIBSnapshotView(UIView *view) {
UIGraphicsBeginImageContextWithOptions(view.bounds.size, YES, [UIScreen mainScreen].scale);
[view drawViewHierarchyInRect:view.bounds afterScreenUpdates:NO];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}

建议:
UIImage *YBIBSnapshotView(UIView *view) {
//这里加上
if(![view isKindOfClass:UIView.class] || view.bounds.size.width<=1||view.bounds.size.height<=1){
return UIImage.new;
}
UIGraphicsBeginImageContextWithOptions(view.bounds.size, YES, [UIScreen mainScreen].scale);
[view drawViewHierarchyInRect:view.bounds afterScreenUpdates:NO];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}

Environment

  • YBImageBrowser version:3.0.9
  • iOS version:ios17
@lifution
Copy link

建议把这个方法改成下面这样

UIImage *YBIBSnapshotView(UIView *view) {
    if (@available(iOS 10.0, *)) {
        UIGraphicsImageRendererFormat *format = [[UIGraphicsImageRendererFormat alloc] init];
        format.scale = [UIScreen mainScreen].scale;
        format.opaque = YES;
        UIGraphicsImageRenderer *renderer = [[UIGraphicsImageRenderer alloc] initWithSize:view.bounds.size format:format];
        UIImage *image = [renderer imageWithActions:^(UIGraphicsImageRendererContext * _Nonnull context) {
            [view drawViewHierarchyInRect:view.bounds afterScreenUpdates:NO];
        }];
        return image;
    } else {
        UIGraphicsBeginImageContextWithOptions(view.bounds.size, YES, [UIScreen mainScreen].scale);
        [view drawViewHierarchyInRect:view.bounds afterScreenUpdates:NO];
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return image;
    }
}

@yangxuefei18
Copy link

yangxuefei18 commented Mar 28, 2024

有用

建议把这个方法改成下面这样

UIImage *YBIBSnapshotView(UIView *view) {
    if (@available(iOS 10.0, *)) {
        UIGraphicsImageRendererFormat *format = [[UIGraphicsImageRendererFormat alloc] init];
        format.scale = [UIScreen mainScreen].scale;
        format.opaque = YES;
        UIGraphicsImageRenderer *renderer = [[UIGraphicsImageRenderer alloc] initWithSize:view.bounds.size format:format];
        UIImage *image = [renderer imageWithActions:^(UIGraphicsImageRendererContext * _Nonnull context) {
            [view drawViewHierarchyInRect:view.bounds afterScreenUpdates:NO];
        }];
        return image;
    } else {
        UIGraphicsBeginImageContextWithOptions(view.bounds.size, YES, [UIScreen mainScreen].scale);
        [view drawViewHierarchyInRect:view.bounds afterScreenUpdates:NO];
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return image;
    }
}

👍

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

3 participants