大数据

压缩图片和相册权限

  • 等比例压缩图片
- (UIImage *)resetSizeSourceImage:(UIImage*)sourceImage {
    CGFloat  scaleToWidth  = 1280;
    if ( scaleToWidth > MAX(sourceImage.size.width,  sourceImage.size.height)) {
        return  sourceImage;
    }
    if (sourceImage.size.width >sourceImage.size.height ) {
        CGFloat height = (scaleToWidth / sourceImage.size.width) * sourceImage.size.height;
        CGRect  rect = CGRectMake(0, 0, scaleToWidth, height);
        UIGraphicsBeginImageContext(rect.size);
        [sourceImage drawInRect:rect];
        UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return image;
    }else{
        CGFloat width = (scaleToWidth / sourceImage.size.height) * sourceImage.size.width;
        CGRect  rect = CGRectMake(0, 0, width, scaleToWidth);
        UIGraphicsBeginImageContext(rect.size);
        [sourceImage drawInRect:rect];
        UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return image;
    }
}
  • 弹出系统相册访问权限(允许或者不允许)
PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
    if (status == PHAuthorizationStatusNotDetermined) {
        [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
            if (status == PHAuthorizationStatusAuthorized) {
               dispatch_sync(dispatch_get_main_queue(), ^{
                   NSLog(@"允许访问");
               });
            }else{
                NSLog(@"拒绝访问");
            }
        }];
    }
  • 判断是否有访问相册的权限
 if (status == PHAuthorizationStatusRestricted || status == PHAuthorizationStatusDenied) {
               //没有访问相册的权限
                return ;
      }else{
     //有访问相册的权限
}