Iphone didFinishPickingMediaWithInfo返回零照片
我正在使用Iphone didFinishPickingMediaWithInfo返回零照片,iphone,uiimage,camera,uiimagepickercontroller,Iphone,Uiimage,Camera,Uiimagepickercontroller,我正在使用 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { [[picker parentViewController] dismissModalViewControllerAnimated:YES]; // MediaType can be kUTTypeImage or kUTT
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
// MediaType can be kUTTypeImage or kUTTypeMovie. If it's a movie then you
// can get the URL to the actual file itself. This example only looks for images.
//
NSString* mediaType = [info objectForKey:UIImagePickerControllerMediaType];
// NSString* videoUrl = [info objectForKey:UIImagePickerControllerMediaURL];
// Try getting the edited image first. If it doesn't exist then you get the
// original image.
//
if (CFStringCompare((CFStringRef) mediaType, kUTTypeImage, 0) == kCFCompareEqualTo) {
UIImage* picture = [info objectForKey:UIImagePickerControllerEditedImage];
if (!picture)
picture = [info objectForKey:UIImagePickerControllerOriginalImage];
// **picture is always nil
// info dictionary count = 1
}
}
发生的情况是,信息字典总是返回一个条目:
{
UIImagePickerController中间类型=
“公众形象”
这是伟大的,但从来没有一个形象
我用这个论坛上的一个很好的例子来做这件事,我很确定这些电话是正确的,但从来没有一张图片
提前感谢!我知道这是几个月后的事了,但我为此挣扎了几个小时,在这个网站和iphonedevsdk.com上都发现了同样的问题,但从来没有找到有效的答案 总而言之,如果该图像是从相机卷/照片库中拾取的,则效果良好,但如果该图像是使用相机拍摄的新照片,则不会起作用。下面介绍如何使其同时适用于这两种情况: 您必须先关闭并释放UIImagePickerController,然后才能尝试对信息字典执行任何操作。要非常清楚: 这不起作用:
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info {
// No good with the edited image (if you allowed editing)
myUIImageView.image = [info objectForKey:UIImagePickerControllerEditedImage];
// AND no good with the original image
myUIImageView.image = [info objectForKey:UIImagePickerControllerOriginalImage];
// AND no doing some other kind of assignment
UIImage *myImage = [info objectForKey:UIImagePickerControllerEditedImage];
[picker dismissModalViewControllerAnimated:YES];
[picker release];
}
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissModalViewControllerAnimated:YES];
[picker release];
// Edited image works great (if you allowed editing)
myUIImageView.image = [info objectForKey:UIImagePickerControllerEditedImage];
// AND the original image works great
myUIImageView.image = [info objectForKey:UIImagePickerControllerOriginalImage];
// AND do whatever you want with it, (NSDictionary *)info is fine now
UIImage *myImage = [info objectForKey:UIImagePickerControllerEditedImage];
}
在所有这些情况下,图像都将为零
然而,通过首先释放UIImagePickerController的魔力
这确实有效:
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info {
// No good with the edited image (if you allowed editing)
myUIImageView.image = [info objectForKey:UIImagePickerControllerEditedImage];
// AND no good with the original image
myUIImageView.image = [info objectForKey:UIImagePickerControllerOriginalImage];
// AND no doing some other kind of assignment
UIImage *myImage = [info objectForKey:UIImagePickerControllerEditedImage];
[picker dismissModalViewControllerAnimated:YES];
[picker release];
}
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissModalViewControllerAnimated:YES];
[picker release];
// Edited image works great (if you allowed editing)
myUIImageView.image = [info objectForKey:UIImagePickerControllerEditedImage];
// AND the original image works great
myUIImageView.image = [info objectForKey:UIImagePickerControllerOriginalImage];
// AND do whatever you want with it, (NSDictionary *)info is fine now
UIImage *myImage = [info objectForKey:UIImagePickerControllerEditedImage];
}
非常简单,但仅此而已。我也有同样的症状,但在我的案例中,我发现UIImagePickerController层次结构中有一个类别覆盖了“uuid”或“setUUID”?我想CoreData需要这些方法,否则它会变得非常、非常混乱,资产库都是CoreData的东西。虽然Matthew Frederick的答案最受欢迎,而且长期以来一直是合适的回答,但从iOS 5.0开始,苹果公司推出了
DismissViewControllerImaged:completion:
,以取代现在已经弃用的(从iOS 6.0开始)解除ViewController激活:
希望在完成块中执行图像信息字典检索对所有人都更有意义
以他上面的例子来说,现在看起来是:
- (void) imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[picker dismissViewControllerAnimated:YES completion:^{
// Edited image works great (if you allowed editing)
myUIImageView.image = [info objectForKey:UIImagePickerControllerEditedImage];
// AND the original image works great
myUIImageView.image = [info objectForKey:UIImagePickerControllerOriginalImage];
// AND do whatever you want with it, (NSDictionary *)info is fine now
UIImage *myImage = [info objectForKey:UIImagePickerControllerEditedImage];
}];
}
某些版本的XCode和Mountain Lion也存在已知的bug。请检查您的控制台以获取以下消息:
2013-01-17 21:36:34.946 3未找到名为“com.apple.persistentrurltranslator.Gatekeeper”的完整ios[5341:1803]服务。assetsd已关闭或配置错误。事情将无法按您期望的方式运行。
如果出现该消息,它可能无法工作。请在实际的iOS设备上检查您的应用程序
无论如何,重新启动模拟器和XCode似乎可以解决这个问题。使用
[[UIImagePickerController alloc] init];
而不是
[[UIImagePickerController alloc] initWithNibName:(NSString *) bundle:(NSBundle *)];
我尝试了以上所有方法,但在iPad 6.0/6.1模拟器上没有成功,但我发现信息中包含“UIImagePickerController参考URL”键,下面是我的代码:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissViewControllerAnimated:YES completion:NULL];
UIImage* image = [info objectForKey:UIImagePickerControllerOriginalImage];
if(NULL == image){
[MyImageLoader loadImageFromAssertByUrl:[info objectForKey:@"UIImagePickerControllerReferenceURL"]
completion:^(UIImage* img){
//img not null here
}];
}else{
//image not null here
}
}
loadImageFromAssertByUrl的代码为:
+(void) loadImageFromAssertByUrl:(NSURL *)url completion:(void (^)(UIImage*)) completion{
ALAssetsLibrary *assetLibrary=[[ALAssetsLibrary alloc] init];
[assetLibrary assetForURL:url resultBlock:^(ALAsset *asset) {
ALAssetRepresentation *rep = [asset defaultRepresentation];
Byte *buffer = (Byte*)malloc(rep.size);
NSUInteger buffered = [rep getBytes:buffer fromOffset:0.0 length:rep.size error:nil];
NSData *data = [NSData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:YES];
UIImage* img = [UIImage imageWithData:data];
completion(img);
} failureBlock:^(NSError *err) {
NSLog(@"Error: %@",[err localizedDescription]);
}];
}
需要遵循以下步骤:
1) 制作UIActionSheet
2) 在选择必要的源类型时-重新加载资源
3) didFinishPickingMediaWithInfo-已接收图像字典中的图像集
- (IBAction)actionButtonUpload:(id)sender {
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self
cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Camera",@"Photo Library", nil];
[actionSheet showInView:self.view];
}
#pragma mark -- UIActionSheet Delegate
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
UIAlertView *alert;
switch (buttonIndex) {
case 0:
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
NSLog(@"No camera!");
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:NULL];
} else {
alert = [[UIAlertView alloc]initWithTitle:@"Alumnikonnect" message:@"Camera is not available, please select a different media" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
}
break;
case 1:
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:NULL];
} else {
alert = [[UIAlertView alloc]initWithTitle:@"Alumnikonnect" message:@"Photolibrary is not available, please select a different media" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
}
break;
}
}
#pragma mark - Image picker delegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
self.imageEventPic.layer.cornerRadius = self.imageEventPic.frame.size.height /2;
self.imageEventPic.layer.masksToBounds = YES;
self.imageEventPic.layer.borderWidth = 0;
self.imageEventPic.layer.borderWidth = 1.0f;
self.imageEventPic.layer.borderColor = [UIColor colorWithRed:0.212 green:0.255 blue:0.302 alpha:1.000].CGColor;
self.labelUploadPic.hidden = YES;
self.imageEventPic.image = info[UIImagePickerControllerOriginalImage];
[self dismissViewControllerAnimated:YES completion:NULL];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self dismissViewControllerAnimated:YES completion:NULL];
}
-(UIImage*)imageWithImage:(UIImage*)image
scaledToSize:(CGSize)newSize;
{
UIGraphicsBeginImageContext( newSize );
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
有人吗?这件事真是祸不单行!!!!你是认真的吗!?!这就是我的问题所在!天哪。_死了_我还没试过,但我想到了几个可能的选择。第一个是复制信息字典--
NSDictionary*infoDictionary=[NSDictionary dictionary dictionary withdictionary:info]
--然后清除选取器--选取器=nil。不确定这是否有效。另一个几乎可以肯定有效的选项是在Xcode中为图像选取代码创建一个单独的文件,并将该文件设置为不使用ARC,将解决方案保留在此处。谢谢..我可以让它工作,但方法是这样的。设置后我关闭相机uiimage。我不知道这可能是ios上的一个bug。哇。我遇到了一个类似的问题,这就完成了工作。现在我很好奇为什么会这样做?我没有看到这个错误,但在模拟器上,它工作的设备上的映像是空的…你确定它的[picker dismissModalViewControllerAnimated:是的完成:
而不是[picker DismissViewControllerInitiated:YES completion:^(void)
?对于我来说,在iPad模拟器(iOS 6.1)中运行iPhone应用程序仍然会产生一个零结果。事实上,这似乎是自iOS 5以来iPad模拟器中的一个长期缺陷,尽管它似乎在真实设备上工作。奇怪。因此,唯一的解决方案是使用资产库…谢谢!我不敢相信我们需要做这种事情来解决至少一年前报告的iPad模拟器中的一个缺陷,但至少它可以我在iPadAir和iOS8上也遇到了同样的问题,我通过这些代码解决了我的问题。谢谢。