压缩图片和相册权限

  • 等比例压缩图片
- (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{
     //有访问相册的权限
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,726评论 25 709
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,261评论 4 61
  • 我是日记星球159号星宝宝周娟,正在参加孙老师的日记星球21天蜕变之旅的写作训练,这是我的第59篇原创日记。你看或...
    周娟168阅读 325评论 0 4
  • 这些日子心情颇有些复杂。今天一大早便到了学校,老天爷有点阴晴不定,时不时下点小雨。突然想起前不久走过的湿地荷花池,...
    上帝老哥阅读 448评论 5 4
  • 把你赶到心底荒芜处,洒上岁月的尘埃,加上绝情的厚盖,而你却在时间的流逝中在里面生根发芽,妖娆攀附在我的整个心脏,怎...
    向阳光阅读 135评论 0 0