压缩图片和相册权限

作者: 傲视苍穹 | 来源:发表于2016-12-15 10:26 被阅读121次
  • 等比例压缩图片
- (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{
     //有访问相册的权限
}

相关文章

网友评论

    本文标题:压缩图片和相册权限

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