UIImagePickerControllerEditedImage用黑色背景保存图像

时间:2018-08-27 03:18:12

标签: ios objective-c uiimagepickercontroller

我正在使用UIImagePickerController保存我的图像。但是,如果我选择一个图像并调整图像的缩放比例,它将保存为不完整的图像(如下所示为黑色背景)。在这种情况下,如何保存适合的图像? 请帮忙。谢谢。 enter image description here

我当前的代码在这里:-

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

    UIImage *image = [info objectForKey:@"UIImagePickerControllerEditedImage"];
    image = [self scaleImage:image toSize:CGSizeMake(200,200)]; // or some other size

    if (image == nil) {
        image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    }

    if (image.imageOrientation != UIImageOrientationUp) {
        image = [self fixOrientation:image];
    }
    if (_imageName==nil || _imageName.length == 0) {

        NSDate *date = [NSDate date];
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateStyle:NSDateFormatterMediumStyle];
        [formatter setTimeStyle:NSDateFormatterShortStyle];
        [formatter setDateFormat:@"YYYY-MM-dd hh:mm:ss"];
        NSString *dateStr = [formatter stringFromDate:date];
        _imageName = [NSString stringWithFormat:@"photo_%@.png",dateStr];
    }

    [[self getCurrentVC] dismissViewControllerAnimated:YES completion:nil];

    if (_delegate && [_delegate respondsToSelector:@selector(selectPhotoManagerDidFinishImage:)]) {
        [_delegate selectPhotoManagerDidFinishImage:image];
    }

    if (_successHandle) {
        _successHandle(self,image);
    }
}

- (UIImage *) scaleImage:(UIImage*)image toSize:(CGSize)newSize {

    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

0 个答案:

没有答案