嘿伙计们我正在使用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];
以上是在尺寸*尺寸视图中设置图像,并在拾取器返回的图像高度小于尺寸时从视图中绘制图像。
以下是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? < / p>
答案 0 :(得分:1)
editdImg获取nil,尝试:
UIImage *editedImg = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
获取文件大小:
- (long long) fileSizeAtPath:(NSString*) filePath{
NSFileManager* manager = [NSFileManager defaultManager];
if ([manager fileExistsAtPath:filePath]){
return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
}
return 0;
}
祝福!
答案 1 :(得分:0)
您可以通过
获取裁剪的图像尺寸UIImage *croppedImg = [MyUtil createUIImageFromUIView:cropView];
NSData *dataForImage = UIImagePNGRepresentation(croppedImg);
现在你可以检查长度
if (dataForImage.length)
{
}
答案 2 :(得分:0)
经过一番搜索,我偶然发现了这个:string value always shows nil in objective-c
这就是为什么我总是在调试模式下看到'nil'而代码运行良好的原因。