iOS从相册获取并压缩图片

从相册获取图片

//打开相册
UIImagePickerController
*pick = [[UIImagePickerController alloc]init];
pick.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
pick.delegate = self;
[self  presentViewController:pick animated:YES completion:nil];

#pragma
#pragma  mark -PICK的代理
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info{
    NSLog(@"%@",info); 
    UIImage *img = info[@"UIImagePickerControllerOriginalImage"]; 
    self.icon.image = img;

     // 回退到app界面
    [self  dismissViewControllerAnimated:YES completion:nil];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    NSLog(@"MDZZ,都进来了也不选头像");
    // 回退到app界面
    [self dismissViewControllerAnimated:YES completion:nil];
}

压缩图片(swift)
通常在获取带相册的图片之后,都会发出去.但是,存在本地的图片可能比较大.因此我们需要将图片压缩

 // 压缩图片 规格为:width:600,等比例压缩
    func getScaleImage(Width:CGFloat, image:UIImage)->UIImage{
        
        if image.size.width < Width{
            return image
        }
        
        let hight = Width/image.size.width * image.size.height
        let rect = CGRect(x: 0, y: 0, width: Width, height: hight)
        // 开启图片上下文
        UIGraphicsBeginImageContext(rect.size)
        // 将图片渲染到图片上下文
        image.drawInRect(rect)
        // 获取图片
        let img = UIGraphicsGetImageFromCurrentImageContext()
        // 关闭图片上下文
        UIGraphicsEndImageContext()
        return img
    }

你可能感兴趣的:(iOS从相册获取并压缩图片)