我正在尝试使用
捕获4.0中返回的图像- (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
}
}
发生的事情是信息字典总是返回一个条目:
{ UIImagePickerControllerMediaType = “public.image”;
这很棒,但从来没有图像。
我在这个论坛上使用了一个很好的例子来做这件事,我很确定这些电话是正确的,但绝不是一张图片。
提前致谢!
答案 0 :(得分:54)
我知道这是几个月之后,但是我在这个问题上挣扎了好几个小时,并且在整个网站和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];
}
在所有这些情况下,图像都是零。
然而,通过首先释放UIImagePickerController 的魔力......
这项工作:
- (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];
}
疯狂简单,但这就是它的全部。
答案 1 :(得分:17)
尽管Matthew Frederick的答案最受欢迎,并且长期以来一直是适当的回应,从iOS 5.0开始,苹果提供dismissViewControllerAnimated:completion:
,以取代现已弃用的(从iOS 6.0开始)dismissViewControllerAnimated:
。< / p>
在完成块中执行图像信息字典检索应该对所有人更有意义。
从上面看他的例子,它现在看起来像:
- (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];
}];
}
答案 2 :(得分:5)
我已经尝试了以上所有,但在iPad 6.0 / 6.1模拟器上没有运气,但是我发现信息包含“UIImagePickerControllerReferenceURL”键,这里是我的代码:
- (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]);
}];
}
答案 3 :(得分:2)
某些版本的XCode和Mountain Lion也存在已知错误。检查您的控制台是否有此消息:
2013-01-17 21:36:34.946 3sure-ios[5341:1803] Named service 'com.apple.PersistentURLTranslator.Gatekeeper' not found. assetsd is down or misconfigured. Things will not work the way you expect them to.
如果出现该消息,它可能无效。在实际的iOS设备上检查您的应用。
尽管如此,重新启动模拟器和XCode似乎解决了这个问题。
答案 4 :(得分:1)
使用
[[UIImagePickerController alloc] init];
而不是
[[UIImagePickerController alloc] initWithNibName:(NSString *) bundle:(NSBundle *)];
答案 5 :(得分:0)
我有相同的症状,但在我的情况下,我发现我在UIImagePickerController层次结构中有一个类别覆盖“uuid”或“setUUID”?我猜CoreData需要这些方法,或者它非常非常困惑,资产库都是CoreData的东西。
答案 6 :(得分:0)
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[picker dismissViewControllerAnimated:YES completion:NULL];
//Image picker for nil value
UIImage* selectedImage = [info objectForKey:UIImagePickerControllerOriginalImage];
NSLog(@"my Info :: %@", info);
if(NULL == selectedImage){
UIImage * selectedImage = [PhotoViewController loadImageFromAssertByUrl:[info objectForKey:@"UIImagePickerControllerReferenceURL"]
completion:^(UIImage * selectedImage){
//img not null here
NSLog(@"img2 ::: %@", selectedImage);
uploadImg.image = selectedImage;
[self.view addSubview:PermissionView];
}];
}else{
//image not null here
NSLog(@"Up IMG00 :: %@", uploadImg.image);
NSLog(@"Sel IMG00 :: %@", selectedImage);
uploadImg.image = [selectedImage retain];
NSLog(@"Up IMG22 :: %@", uploadImg.image);
NSLog(@"Sel IMG22 :: %@", selectedImage);
}
}
+(UIImage *) loadImageFromAssertByUrl:(NSURL *)url completion:(void (^)(UIImage*)) completion{
__block UIImage* img;
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];
img = [UIImage imageWithData:data];
completion(img);
NSLog(@"img ::: %@", img);
} failureBlock:^(NSError *err) {
NSLog(@"Error: %@",[err localizedDescription]);
}];
return img;
}
答案 7 :(得分:-1)
需要遵循以下步骤:
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;
}