美文网首页
x、iOS保存图片至相册原生代码

x、iOS保存图片至相册原生代码

作者: 万士辰 | 来源:发表于2017-05-17 14:34 被阅读118次
    #import <UIKit/UIKit.h>
    
    @implementation Untility : NSObject 
    static Untility *instance = nil;
    +(Untility *)sharedInstance{
        @synchronized(self) {
            if(instance == nil) {
                instance = [[[self class] alloc] init];
            }
        }
        return instance;
    }
    
    - (void)loadImageFinished:(UIImage *)image
    {
        UIImageWriteToSavedPhotosAlbum(image, self,
                                       @selector(image:didFinishSavingWithError:contextInfo:),
                                       (__bridge void *)self);
    }
    
    - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
    {
        //初始化提示框;
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"保存成功" preferredStyle:  UIAlertControllerStyleAlert];
        
        [alert addAction:[UIAlertAction actionWithTitle:@"确定"
                                                  style:UIAlertActionStyleDefault
                                                handler:^(UIAlertAction * _Nonnull action) {}]];
        
        //弹出提示框;
        [[self getCurrentVC] presentViewController:alert animated:true completion:nil];
    }
    
    - (UIViewController *)getCurrentVC
    {
        UIViewController *result = nil;
        
        UIWindow * window = [[UIApplication sharedApplication] keyWindow];
        if (window.windowLevel != UIWindowLevelNormal)
        {
            NSArray *windows = [[UIApplication sharedApplication] windows];
            for(UIWindow * tmpWin in windows)
            {
                if (tmpWin.windowLevel == UIWindowLevelNormal)
                {
                    window = tmpWin;
                    break;
                }
            }
        }
        
        UIView *frontView = [[window subviews] objectAtIndex:0];
        id nextResponder = [frontView nextResponder];
        
        if ([nextResponder isKindOfClass:[UIViewController class]])
            result = nextResponder;
        else
            result = window.rootViewController;
        
        return result;
    }
    
    @end
    
    #include <sys/socket.h>
    #include <netdb.h>
    #include <arpa/inet.h>
    #include <err.h>
    extern "C"{
        void SaveImageToSystemAlbum(const char* path){
            UIImage* image = [UIImage imageWithContentsOfFile:[NSString stringWithUTF8String:path]];
            [[Untility sharedInstance] loadImageFinished:image];
        }
    }
    

    相关文章

      网友评论

          本文标题:x、iOS保存图片至相册原生代码

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