我正在尝试执行我认为简单的任务,即调整通过UIImagePickerControllerSourceTypeCamera刚拍摄的图像的大小。

拍照后,我想立即将其缩小。我可以保持宽高比并将其缩小到25%。在这里,我提取刚刚拍摄的图像(称为cameraImage)。

任何帮助都将是美好的!谢谢你们!

-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info {

    cameraImage = [info objectForKey:UIImagePickerControllerOriginalImage];
    [[picker presentingViewController] dismissViewControllerAnimated:YES
    completion:^{[self photoUpload];}];

}

最佳答案

UIImage *scaleDownImg = [ClassName imageWithImage: cameraImage scaledToSize:CGSizeMake(20, 20)];

+ (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
    //UIGraphicsBeginImageContext(newSize);
    // In next line, pass 0.0 to use the current device's pixel scaling factor (and thus account for Retina resolution).
    // Pass 1.0 to force exact pixel size.
    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

10-08 16:56