-(void)Open_Library
{
// Create image picker controller
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
// Set source to the camera
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.delegate=self;
// Delegate is self
// Allow editing of image ?
imagePicker.allowsEditing = YES;
// Show image picker
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
{
popover = [[UIPopoverController alloc] initWithContentViewController:imagePicker];
CGRect popoverRect = CGRectMake(10, 10, 200, 200);
[popover presentPopoverFromRect:popoverRect inView:self.imgview permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
else
{
[self presentViewController:imagePicker animated:YES completion:nil];
}
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage: (UIImage *)img editingInfo:(NSDictionary *)editingInfo
{
_imgview.image = img;
[picker dismissModalViewControllerAnimated:YES];
}
此处图片在图库中打开但未选择。这里的“imgview”是用于显示从图库中选择的图像的IMAGEVIEW。我在这里做错了什么。我想显示从画廊中选择的图像。
答案 0 :(得分:1)
- (IBAction)onUploadImage:(id)sender
{
[self pickphoto:UIImagePickerControllerSourceTypePhotoLibrary];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
[imagePicker dismissModalViewControllerAnimated:NO];
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
{
if ([self.popoverController isPopoverVisible])
{
[self.popoverController dismissPopoverAnimated:YES];
[popoverController release];
}
if (image != nil)
{
myImage.image = image;
}
}
}
- (void)pickphoto:(UIImagePickerControllerSourceType)sourceType
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
{
if ([self.popoverController isPopoverVisible])
{
[self.popoverController dismissPopoverAnimated:YES];
[popoverController release];
}
else
{
if ([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypePhotoLibrary])
{
imagePicker =[[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
//imagePicker.mediaTypes = [NSArray arrayWithObjects:(NSString *) kUTTypeImage,(NSString *) kUTTypeMovie,nil];
imagePicker.allowsEditing = NO;
self.popoverController = [[UIPopoverController alloc]initWithContentViewController:imagePicker];
popoverController.delegate = self;
//[self.popoverController presentPopoverFromBarButtonItem: permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
[self.popoverController presentPopoverFromRect:CGRectMake(0,900,320,800) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
// [imagePicker release];
}
}
}
else
{
imagePicker = [[UIImagePickerController alloc] init];
imagePicker.navigationBar.tintColor=[UIColor blackColor];
imagePicker.navigationItem.title=@"Photo Albums";
imagePicker.delegate = self;
imagePicker.sourceType = sourceType;
//[self presentModalViewController:imagePicker animated:YES];
[self presentViewController:imagePicker animated:YES completion:nil];
}
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self dismissViewControllerAnimated:imagePicker completion:nil];
//[imagePicker dismissModalViewControllerAnimated:YES];
}