Sim*_*iwi 1 camera objective-c uiimagepickercontroller ios
这是我提供UIImagePickerController的代码:
// Show the media browser with our settings, then the browser will call our delegate if needed
- (BOOL) startMediaBrowserFromViewController: (UIViewController*) controller
usingDelegate: (id <UIImagePickerControllerDelegate,
UINavigationControllerDelegate>) delegate {
if (([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypeSavedPhotosAlbum] == NO)
|| (delegate == nil)
|| (controller == nil))
return NO;
UIImagePickerController *mediaUI = [[[UIImagePickerController alloc] init] autorelease];
mediaUI.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
// Check for images type
NSArray * availableTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeSavedPhotosAlbum];
BOOL imgsOnlyAvailable = NO;
// Check if we have only images
for (int i = 0; i < [availableTypes count]; i++) {
// Convert the type
CFStringRef type = (CFStringRef) [availableTypes objectAtIndex:i];
if (CFStringCompare ((CFStringRef) type, kUTTypeImage, 0) == kCFCompareEqualTo) {
// We have images
imgsOnlyAvailable = YES;
break;
}
}
// Check if they are available
if (imgsOnlyAvailable == NO)
return NO;
// Displays only saved pictures from the Camera Roll album.
mediaUI.mediaTypes = [NSArray arrayWithObject:(id) kUTTypeImage];
// Hides the controls for moving & scaling pictures, or for
// trimming movies. To instead show the controls, use YES.
mediaUI.allowsEditing = NO;
mediaUI.delegate = delegate;
[controller presentModalViewController: mediaUI animated: YES];
return YES;
}
// Picker delegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
UIImage *originalImage;
// Handle a still image picked from a photo album
if (CFStringCompare ((CFStringRef) mediaType, kUTTypeImage, 0)
== kCFCompareEqualTo) {
originalImage = (UIImage *) [info objectForKey:
UIImagePickerControllerOriginalImage];
// Set image
self.imgPic.image = originalImage;
// Now set the button to enabled
self.btnToText.enabled = YES;
}
// Hide picker selector
[[picker parentViewController] dismissModalViewControllerAnimated: YES];
[picker release];
}
Run Code Online (Sandbox Code Playgroud)
问题是每当我尝试单击UIImagePickerController中的图像时没有任何反应(通常它选择图像然后将关闭控制器)
有没有理由为什么会出现这样的错误?
谢谢!
// Select the image
- (IBAction)btnSelectFileTouchUp:(id)sender {
// Start the media browser
if ([self startMediaBrowserFromViewController:self usingDelegate:self] == NO) {
// Can't open the media browser
UIAlertView * alrt = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Can't open the photo media browser in this device" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alrt show];
[alrt release];
}
}
Run Code Online (Sandbox Code Playgroud)
检查出的文档parentViewController中的UIViewController类参考.特别是这个小小的花絮.
在iOS 5.0之前,如果视图没有父视图控制器并且以模态方式呈现,则将返回呈现它的视图控制器.这已不再是这种情况.您可以使用presentViewController属性获取呈现视图控制器.
在这个例子中,我希望parentViewControlleriOS 5上的nil为nil,这就是控制器不会解散的原因.尝试更换parentViewController用presentingViewController.
更新:您必须检查是否存在presentingViewControlleron UIViewController以提供iOS版本<5.0的行为.
UIViewController *dismissingController = nil;
if ([self respondsToSelector:@selector(presentingViewController)])
dismissingController = self.presentingViewController;
else
dismissingController = self.parentViewController;
[dismissingController dismissModalViewControllerAnimated:YES];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1668 次 |
| 最近记录: |