嘿,伙计们,我正在用UIImagePickerController做一些图像编辑。以下是imagePickerController:didFinishPickingMediaWithInfo
中的一些代码
UIImage *editedImg = [info objectForKey:UIImagePickerControllerEditedImage];
UIImageView *imgView = [[UIImageView alloc] initWithImage:editedImg];
CGRect imgFrm = imgView.frame;
float rate = imgFrm.size.height / imgFrm.size.width;
imgFrm.size.width = size;
imgFrm.size.height = size * rate;
imgFrm.origin.x = 0;
imgFrm.origin.y = (size - imgFrm.size.height) / 2;
[imgView setFrame:imgFrm];
UIView *cropView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, size, size)];
[cropView setBackgroundColor:[UIColor blackColor]];
[cropView addSubview:imgView];
UIImage *croppedImg = [MyUtil createUIImageFromUIView:cropView];
上面是在size*size视图中设置图像,并在picker返回的图像高度小于size时从视图中绘制图像。
下面是createUIImageFromUIView:(UIView*)view
的代码:
+ (UIImage *)createUIImageFromUIView:(UIView *)view
{
UIGraphicsBeginImageContextWithOptions(view.frame.size, NO, 2.0);
CGContextRef ctx = UIGraphicsGetCurrentContext();
[view.layer renderInContext:ctx];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return viewImage;
}
我的问题是:在调试时,‘editedImg’(在第一行定义)只显示'nil‘。但是,下面的代码运行得很好。我正确地获得了corpView(也显示为'nil‘),得到了裁剪的图像,并可以将其编码为base64编码的字符串,以便发送到服务器端。我只想知道为什么editedImg是nil (由info objectForKey:UIImagePickerControllerEditedImage,返回,但当我选择在调试模式下打印信息时,控制台中的输出不是nil)?
https://stackoverflow.com/questions/20464415
复制相似问题