iphone 怎么使用图片选取器 UIImagePickerController

 1 #import <UIKit/UIKit.h>
 2 
 3 @interface ViewController : UIViewController <UINavigationControllerDelegate,UIImagePickerControllerDelegate>{
 4     UIImageView *backImageView;
 5 }
 6 
 7 @property(retain, nonatomic) IBOutlet UIButton *showImgPickerButton;
 8 @property(retain, nonatomic) IBOutlet UIImageView *backImageView;
 9 
10 - (IBAction)showImagePicker:(id)sender;
11 
12 @end
  1 //
  2 //  ViewController.m
  3 //  test6
  4 //
  5 //  Created by Totem on 12-7-2.
  6 //  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
  7 //
  8 
  9 #import "ViewController.h"
 10 
 11 @implementation ViewController
 12 
 13 @synthesize showImgPickerButton;
 14 @synthesize backImageView;
 15 
 16 - (IBAction)showImagePicker:(id)sender
 17 {
 18 #if TARGET_IPHONE_SIMULATOR
 19     UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
 20 #else
 21     UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
 22 #endif
 23     if ([UIImagePickerController isSourceTypeAvailable:sourceType]) {
 24         NSLog(@"调用UIImagePickerController");
 25         UIImagePickerController *picker = [[[UIImagePickerController alloc] init] autorelease];
 26         picker.delegate = self;
 27         picker.allowsEditing = YES;
 28         
 29         [self presentModalViewController:picker animated:YES];
 30 //        [self.navigationController pushViewController:picker animated:YES];
 31     }else {
 32         NSLog(@"为调用UIImagePickerController");
 33     }
 34 }
 35 
 36 //两种完成选取后调用方式之一
 37 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
 38 {
 39     NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:editingInfo];
 40     [dic setObject:image forKey:@"UIImagePickerControllerEditedImage"];
 41     UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
 42     [self dismissModalViewControllerAnimated:YES];
 43     [backImageView setImage:image];
 44 }
 45 
 46 //两种完成选取后调用方式之一
 47 //- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
 48 //    if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
 49 //        NSLog(@"来自Camera相机的图片");
 50 //        //如果是 来自照相机的image,那么先保存  
 51 //        UIImage* original_image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];  
 52 //        UIImageWriteToSavedPhotosAlbum(original_image, self,   
 53 //                                       @selector(image:didFinishSavingWithError:contextInfo:),   
 54 //                                       nil);
 55 //    }else if(picker.sourceType == UIImagePickerControllerSourceTypePhotoLibrary){
 56 //        NSLog(@"来自PhotoLibrary");
 57 //    }else if(picker.sourceType == UIImagePickerControllerSourceTypeSavedPhotosAlbum){
 58 //        NSLog(@"来自");
 59 //    }
 60 //    //获得编辑过的图片  
 61 //    UIImage* image = [info objectForKey: @"UIImagePickerControllerEditedImage"];
 62 //    [self dismissModalViewControllerAnimated:YES];
 63 //    [backImageView setImage:image];
 64 //}
 65 
 66 - (void)image:(UIImageView*)image didFinishSavingWithError:(NSString*)error contextInfo:(NSString*)
 67 context{
 68     NSLog(@"保存完成!");
 69 }
 70 
 71 - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
 72     [self dismissModalViewControllerAnimated:YES];
 73 }
 74 
 75 - (void)dealloc
 76 {
 77     [showImgPickerButton release];
 78     [backImageView release];
 79     [super dealloc];
 80 }
 81 
 82 - (void)viewDidLoad
 83 {
 84     [super viewDidLoad];
 85     NSError *error = [[[NSError alloc] init] autorelease];
 86     
 87     CFShow([[NSFileManager defaultManager] contentsOfDirectoryAtPath:[NSHomeDirectory() stringByAppendingString:@"/documents"] error:&error]);
 88     // Do any additional setup after loading the view, typically from a nib.
 89 }
 90 
 91 - (void)viewDidUnload
 92 {
 93     [super viewDidUnload];
 94     // Release any retained subviews of the main view.
 95 }
 96 
 97 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
 98 {
 99     return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
100 }
101 
102 @end

 


xib文件中只有一个UIImageView作为背景,还有一个按钮UIButton用来显示UIImagePickerController

 


posted @ 2012-07-03 11:10  GreyWolf  阅读(323)  评论(0编辑  收藏  举报