如何使这项工作适用于iPad?它在iPhone上工作正常,但Apple要求所有应用程序都可以在ipad上运行。对此有何帮助?
- (IBAction)chooseImage:(id)sender
{
self.imagePicker = [[UIImagePickerController alloc] init];
self.imagePicker.delegate = self;
[self.imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:self.imagePicker animated:YES completion:nil];
}
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
self.image = info[UIImagePickerControllerOriginalImage];
[self.imageView setImage:self.image];
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void) imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self dismissViewControllerAnimated:YES completion:nil];
}
现在我已经走到了这一步!当我使用ipad从照片库中选择图像时,它现在不再崩溃了。问题是图像没有被选中?
有什么建议吗?
- (IBAction)chooseImage:(id)sender
{
self.imagePicker = [[UIImagePickerController alloc] init];
self.imagePicker.delegate = self;
[self.imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
_popover = [[UIPopoverController alloc] initWithContentViewController:_imagePicker];
_popover.delegate = self;
[_popover setPopoverContentSize:CGSizeMake(320, 460)];
[_popover presentPopoverFromBarButtonItem:[[UIBarButtonItem alloc]initWithCustomView:(UIButton*)sender] permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
[self presentViewController:self.imagePicker animated:YES completion:nil];
}
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
if
(self.popover && self.popover.popoverVisible) {
[self.popover dismissPopoverAnimated:YES];
}
else
self.image = info[UIImagePickerControllerOriginalImage];
[self.imageView setImage:self.image];
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void) imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self dismissViewControllerAnimated:YES completion:nil];
}
答案 0 :(得分:0)
以下是我在UIImagePickerController上的书中的部分:
http://www.apeth.com/iOSBook/ch30.html#_uiimagepickercontroller
好的,抱歉,没有通用代码。但它在结构上与MPMediaPickerController的工作方式相同,我为此提供了代码,展示了如何在iPhone和iPad上执行此操作:
http://www.apeth.com/iOSBook/ch29.html#EXmodalOrPop
这很棘手,因为弹出窗口管理与呈现的视图控制器管理非常不同。该代码向您展示了一些您需要担心的内容......
以下是我刚刚撰写的通用应用中的实际代码:
- (IBAction)doPickAPicture:(id)sender {
UIImagePickerControllerSourceType type = UIImagePickerControllerSourceTypePhotoLibrary;
BOOL ok = [UIImagePickerController isSourceTypeAvailable:type];
if (!ok) {
return;
}
UIImagePickerController* picker = [UIImagePickerController new];
picker.sourceType = type;
picker.mediaTypes = @[(__bridge NSString*)kUTTypeImage];
picker.delegate = self;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
[self presentViewController:picker animated:YES completion:nil];
else {
UIPopoverController* pop = [[UIPopoverController alloc] initWithContentViewController:picker];
[pop presentPopoverFromRect:[sender bounds] inView:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
self.currentPop = pop;
}
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[self dismissViewControllerAnimated:YES completion:nil];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
if (self.currentPop && self.currentPop.popoverVisible) {
[self.currentPop dismissPopoverAnimated:YES];
} else {
[self dismissViewControllerAnimated:YES completion:nil];
}
[CATransaction setCompletionBlock:^{
self.currentPop = nil;
// do more stuff
}];
}