我在iPad上遇到错误/崩溃只在使用以下代码时,iPhone工作正常。
基本上是在尝试加载图库以选择图像时。
调试错误是; “在iPad上,UIImagePickerController必须通过UIPopoverController呈现'”
-(void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 0) {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:picker animated:YES];
[picker release];
}
else {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Error accessing photo library"
message:@"Device does not support a photo library"
delegate:nil
cancelButtonTitle:@"Abort!"
otherButtonTitles:nil];
[alert show];
[alert release];
}
}
if (buttonIndex == 1) {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType =UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:picker animated:YES];
[picker release];
}
else {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Error accessing Camera"
message:@"Device does not support a Camera"
delegate:nil
cancelButtonTitle:@"Abort!"
otherButtonTitles:nil];
[alert show];
[alert release];
}
}
}
-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
NSLog(@"the info opf picker is %@",info);
NSAutoreleasePool *pool=[[NSAutoreleasePool alloc]init];
NSData *imageData;
if ([info objectForKey:UIImagePickerControllerEditedImage]) {
UIImage *image2=[info objectForKey:UIImagePickerControllerEditedImage];
//UIImage *myscaledImage=[self scaleImage:image2 toSize:CGSizeMake(18, 24)];
//imageData = UIImagePNGRepresentation(myscaledImage);
imageData=UIImageJPEGRepresentation(image2, .4);
NSLog(@"the lenght of the edited image data is %d",[imageData length]);
}
else {
UIImage *image=[info objectForKey:UIImagePickerControllerOriginalImage];
//UIImage *myscaledImage=[self scaleImage:image toSize:CGSizeMake(18, 24)];
//imageData = UIImagePNGRepresentation(myscaledImage);
imageData=UIImageJPEGRepresentation(image, .6);
NSLog(@"the lenght of the image dataa is %d",[imageData length]);
}
NSDictionary *tempDict=[NSDictionary dictionaryWithObjectsAndKeys:imageData,@"Image",@"Theme",@"Cell_Text",nil];
// NSLog(@"the custom array%@ and the row =%d", customArray,currentCameraRow);
[customArray replaceObjectAtIndex:currentCameraRow withObject:tempDict];
[pool drain];
//NSLog(@"")
[self startThread];
[picker dismissModalViewControllerAnimated:YES];
[myCustomTableView reloadData];
NSLog(@"size of myObject: %zd", malloc_size(myCustomTableView));
}
任何人都可以帮助上面的代码吗?我猜一个if / else语句但是没有第一个想法它会是什么以及放在哪里。
提前致谢,
克里斯
编辑 - 好的我现在让它工作从画廊加载现在加载'照片'在一个小窗口,我可以选择,但是没有'选择'或'选择'按钮,如果我点击照片,那里是没有勾选,说它被选中,但它确实选择它,但要解雇我点击窗外...所以它的工作原理,但我猜我错过了一些东西,这里是代码;
-(void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 0) {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
if(popoverController != nil)
{
[popoverController release];
popoverController = nil;
}
UIPopoverController* popover = [[UIPopoverController alloc] initWithContentViewController:picker];
popoverController = popover;
popoverController.delegate = self;
[popover presentPopoverFromRect:CGRectMake(0, 0, 200, 800)
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
}
else
{
[self presentModalViewController:picker animated:YES];
}
// [self presentModalViewController:picker animated:YES];
[picker release];
}
else {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Error accessing photo library"
message:@"Device does not support a photo library"
delegate:nil
cancelButtonTitle:@"Abort!"
otherButtonTitles:nil];
[alert show];
[alert release];
}
}
if (buttonIndex == 1) {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType =UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:picker animated:YES];
[picker release];
}
else {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Error accessing Camera"
message:@"Device does not support a Camera"
delegate:nil
cancelButtonTitle:@"Abort!"
otherButtonTitles:nil];
[alert show];
[alert release];
}
}
}
-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
NSLog(@"the info opf picker is %@",info);
NSAutoreleasePool *pool=[[NSAutoreleasePool alloc]init];
NSData *imageData;
if ([info objectForKey:UIImagePickerControllerEditedImage]) {
UIImage *image2=[info objectForKey:UIImagePickerControllerEditedImage];
//UIImage *myscaledImage=[self scaleImage:image2 toSize:CGSizeMake(18, 24)];
//imageData = UIImagePNGRepresentation(myscaledImage);
imageData=UIImageJPEGRepresentation(image2, .4);
NSLog(@"the lenght of the edited image data is %d",[imageData length]);
}
else {
UIImage *image=[info objectForKey:UIImagePickerControllerOriginalImage];
//UIImage *myscaledImage=[self scaleImage:image toSize:CGSizeMake(18, 24)];
//imageData = UIImagePNGRepresentation(myscaledImage);
imageData=UIImageJPEGRepresentation(image, .6);
NSLog(@"the lenght of the image dataa is %d",[imageData length]);
}
NSDictionary *tempDict=[NSDictionary dictionaryWithObjectsAndKeys:imageData,@"Image",@"Theme",@"Cell_Text",nil];
// NSLog(@"the custom array%@ and the row =%d", customArray,currentCameraRow);
[customArray replaceObjectAtIndex:currentCameraRow withObject:tempDict];
[pool drain];
//NSLog(@"")
[self startThread];
[picker dismissModalViewControllerAnimated:YES];
[myCustomTableView reloadData];
NSLog(@"size of myObject: %zd", malloc_size(myCustomTableView));
}
-(UIImage *)scaleImage:(UIImage *)image toSize:(CGSize)newSize {
UIGraphicsBeginImageContext(newSize);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self dismissModalViewControllerAnimated:YES];
}
答案 0 :(得分:0)
错误非常明确:
在iPad上,必须通过UIPopoverController'
呈现UIImagePickerController
在iPad上运行时,您需要将图像选择器放在弹出控制器中。当sourceType
用于照片库时,情况就是如此。基于摄像头的图像选择器可以显示为全屏模态视图控制器。
这不是iPhone上的问题,因为iPhone不支持UIPopoverController
。
示例应用PrintPhoto
演示如何在弹出窗口中显示图像选择器。具体来说,请参阅PrintPhotoViewController.m
。
答案 1 :(得分:0)
做这样的事情:
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
//present as a pop over vc
} else {
//present as a modal vc
}
要呈现弹出窗口,您可以使用此方法
- (void)presentPopoverFromRect:(CGRect)rect inView:(UIView *)view permittedArrowDirections:(UIPopoverArrowDirection)arrowDirections animated:(BOOL)animated
您可以通过此链接link
阅读文档