美文网首页iOS 进阶iOS开发你需要知道的
iOS如何在UIWindow上弹出UIAlertControll

iOS如何在UIWindow上弹出UIAlertControll

作者: 最强的小强 | 来源:发表于2019-04-17 16:06 被阅读281次

可以设置新的UIWindow的优先级

- (void)longPress:(UITapGestureRecognizer *)recognizer {
//    长按保存图片至相册
    if (recognizer.state == UIGestureRecognizerStateBegan) {
       // 关键代码
        UIImage *image = self.fromTheImageView.image;
        UIWindow *alertWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
        alertWindow.rootViewController = [UIViewController new];
        alertWindow.windowLevel = UIWindowLevelAlert + 1;
        [alertWindow makeKeyAndVisible];
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"保存图片" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
//                保存图片至相册
                [PHAssetChangeRequest creationRequestForAssetFromImage:image];
            } completionHandler:^(BOOL success, NSError * _Nullable error) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    if (success) {
                        [TipUtils showToast:self message:@"图片成功保存到相册"];
//                        [self dismiss];
                    }
                    NSLog(@"%@",success ? @"保存成功" : @"保存失败");
                });
            }];
        }];
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        [alert addAction:action];
        [alert addAction:cancel];
        [alertWindow.rootViewController presentViewController:alert animated:YES completion:nil];
    }
}

相关文章

网友评论

    本文标题:iOS如何在UIWindow上弹出UIAlertControll

    本文链接:https://www.haomeiwen.com/subject/ankzbqtx.html