保存截图到相册 oc
+ (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    NSString* path = (__bridge_transfer NSString *)(contextInfo);
    [[NSFileManager defaultManager] removeItemAtPath:path error:nil];

    NSLog(@"保存结束");
    if( error == nil )
    {
    }
    else
    {
        NSLog( @"Error saving image with UIImageWriteToSavedPhotosAlbum: %@", error );
    }
}
int _SavePhoto(char *readAddr)
{
    int result = 0;
    #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 140000
        if( CHECK_IOS_VERSION( @"14.0" ) )
        {
            result = [__NtNotificationWrapper requestPhotoPermissionNewest];
        }
        else
    #endif
            result = [__NtNotificationWrapper requestPhotoPermissionNew];
    if(result != 1)
    {
        return result;
    }
    
    NSString *strReadAddr = [NSString stringWithUTF8String:readAddr];
    UIImage *img = [UIImage imageWithContentsOfFile:strReadAddr];
    NSLog([NSString stringWithFormat:@"w:%f, h:%f", img.size.width, img.size.height]);
    UIImageWriteToSavedPhotosAlbum(img, nil, 
        @selector(image:didFinishSavingWithError:contextInfo:), nil);

    return 1;
}

+ (int)requestPhotoPermissionNew
{
    PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
    
    if( status == PHAuthorizationStatusAuthorized )
        return 1;
    else if(status == PHAuthorizationStatusNotDetermined)
    {
        __block BOOL authorized = NO;
        
        dispatch_semaphore_t sema = dispatch_semaphore_create( 0 );
        [PHPhotoLibrary requestAuthorization:^( PHAuthorizationStatus status )
        {
            authorized = ( status == PHAuthorizationStatusAuthorized );
            dispatch_semaphore_signal( sema );
        }];
        dispatch_semaphore_wait( sema, DISPATCH_TIME_FOREVER );
        
        return authorized ? 1 : 0;
    }
    
    return 0;
}

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 140000
+ (int)requestPhotoPermissionNewest
{
    PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatusForAccessLevel:PHAccessLevelReadWrite];
    
    if( status == PHAuthorizationStatusAuthorized )
        return 1;
    else if( status == PHAuthorizationStatusLimited )
    {
        return 2;
    }
    else if( status == PHAuthorizationStatusNotDetermined )
    {
        [PHPhotoLibrary requestAuthorizationForAccessLevel:PHAccessLevelReadWrite handler:^( PHAuthorizationStatus status )
        {

        }];
        return 3;
    }
    return 0;
}
#endif



首页 我的博客
粤ICP备17103704号