//IOS主要用的是UIImagePickerControllerDelegate这个事件委托来实现照相以及进入照片库的 @protocol UIImagePickerControllerDelegate<NSObject> @optional // The picker does not dismiss itself; the client dismisses it in these callbacks. // The delegate will receive one or the other, but not both, depending whether the user // confirms or cancels. - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo __OSX_AVAILABLE_BUT_DEPRECATED(__MAC_NA,__MAC_NA,__IPHONE_2_0,__IPHONE_3_0); - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info; - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker; @end 应用ActionSheet弹出框 弹出三个按钮,共用户选择 1:第一步: //取得设备型号 #import <sys/types.h> #import <sys/sysctl.h> - (NSString *) platform{ size_t size; sysctlbyname("hw.machine", NULL, &size, NULL, 0); char *machine = malloc(size); sysctlbyname("hw.machine", machine, &size, NULL, 0); NSString *platform = [NSString stringWithUTF8String:machine]; free(machine); return platform; } - (NSString *) platformString{ NSString *platform = [self platform]; if ([platform isEqualToString:@"iPhone1,1"]) return @"iPhone 1G"; if ([platform isEqualToString:@"iPhone1,2"]) return @"iPhone 3G"; if ([platform isEqualToString:@"iPhone2,1"]) return @"iPhone 3GS"; if ([platform isEqualToString:@"iPhone3,1"]) return @"iPhone 4"; if ([platform isEqualToString:@"iPhone3,2"]) return @"Verizon iPhone 4"; if ([platform isEqualToString:@"iPod1,1"]) return @"iPod Touch 1G"; if ([platform isEqualToString:@"iPod2,1"]) return @"iPod Touch 2G"; if ([platform isEqualToString:@"iPod3,1"]) return @"iPod Touch 3G"; if ([platform isEqualToString:@"iPod4,1"]) return @"iPod Touch 4G"; if ([platform isEqualToString:@"iPad1,1"]) return @"iPad"; if ([platform isEqualToString:@"iPad2,1"]) return @"iPad 2 (WiFi)"; if ([platform isEqualToString:@"iPad2,2"]) return @"iPad 2 (GSM)"; if ([platform isEqualToString:@"iPad2,3"]) return @"iPad 2 (CDMA)"; if ([platform isEqualToString:@"i386"]) return @"Simulator"; return platform; } 2:第二步: 实现UIActionSheetDelegate委托方法 @protocol UIActionSheetDelegate <NSObject> @optional // Called when a button is clicked. The view will be automatically dismissed after this call returns - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex; // Called when we cancel a view (eg. the user clicks the Home button). This is not called when the user clicks the cancel button. // If not defined in the delegate, we simulate a click in the cancel button - (void)actionSheetCancel:(UIActionSheet *)actionSheet; - (void)willPresentActionSheet:(UIActionSheet *)actionSheet; // before animation and showing view - (void)didPresentActionSheet:(UIActionSheet *)actionSheet; // after animation - (void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex; // before animation and hiding view - (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex; // after animation @end //选择ActionSheet的初始化,并弹出ActionSheet -(IBAction) chooseHeadPhoto{ UIActionSheet *actionSheet=[[UIActionSheet alloc] init]; if (deviceType == @"Simulator" || deviceType == @"iPad"||deviceType == @"iPod Touch 1G" ||deviceType == @"iPod Touch 2G"||deviceType== @"iPod Touch 3G") { if (contactsAdd.Photo != nil) actionSheet=[[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Delete this Photo" otherButtonTitles:@"Choose Photo",nil]; else { actionSheet=[[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Choose Photo",nil]; } } else { if (contactsAdd.Photo != nil) actionSheet=[[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Delete this Photo" otherButtonTitles:@"Choose Photo",@"Take Photo",nil]; else { actionSheet=[[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Choose Photo",@"Take Photo",nil]; } } [actionSheet showInView:self.view]; [actionSheet release]; } //实现委托方法中的clickedButtonAtIndex - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{ if(!(deviceType == @"Simulator" || deviceType == @"iPad"||deviceType == @"iPod Touch 1G"||deviceType == @"iPod Touch 2G"||deviceType== @"iPod Touch 3G")) { //有头像 if (contactsAdd.Photo != nil) { //返回 if(buttonIndex == 3) { return; } //删除 if(buttonIndex==0) { NSString *imagePath = [NSString stringWithFormat:@"%@/%@.jpg", self.documentsPath, contactsAdd.Photo]; NSError *error = nil; if([fileManager fileExistsAtPath:imagePath]) { [fileManager removeItemAtPath:imagePath error:&error]; } contactsAdd.Photo = nil; // headImageView.image = nil; // headImageView.image=[UIImage imageNamed:@"nophoto65px.png"]; } //选择 if(buttonIndex == 1) { UIImagePickerController *picker = [[UIImagePickerController alloc] init]; if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { [picker.navigationBar setTintColor:[UIColor colorWithRed:222.0/255.0 green:109.0/255.0 blue:144.0/255.0 alpha:1.0]]; //给navigationBar设置背景图片 if ([picker.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)]) { [picker.navigationBar setBackgroundImage:[UIImage imageNamed:@"64px.png"] forBarMetrics:UIBarMetricsDefault]; } [picker.navigationBar setFrame:CGRectMake(0, 0, 540, 60)]; picker.navigationBar.layer.contents = (id)[UIImage imageNamed:@"64px.png"].CGImage; } else { [picker.navigationBar setTintColor:[UIColor colorWithRed:208.0/255.0 green:75.0/255.0 blue:109.0/255.0 alpha:1.0]]; //给navigationBar设置背景图片 if ([picker.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)]) { [picker.navigationBar setBackgroundImage:[UIImage imageNamed:@"a_nav_bg.png"] forBarMetrics:UIBarMetricsDefault]; } } picker.delegate = self; if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { [picker setModalPresentationStyle:UIModalPresentationFormSheet]; } [self presentModalViewController:picker animated:YES]; if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { [picker.view.superview setFrame:CGRectMake(113, 175, picker.view.frame.size.width, 680)]; } [picker release]; } //取像 if(buttonIndex == 2) { UIImagePickerController *picker = [[UIImagePickerController alloc] init]; if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { [picker.navigationBar setTintColor:[UIColor colorWithRed:222.0/255.0 green:109.0/255.0 blue:144.0/255.0 alpha:1.0]]; //给navigationBar设置背景图片 if ([picker.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)]) { [picker.navigationBar setBackgroundImage:[UIImage imageNamed:@"64px.png"] forBarMetrics:UIBarMetricsDefault]; } [picker.navigationBar setFrame:CGRectMake(0, 0, 540, 60)]; picker.navigationBar.layer.contents = (id)[UIImage imageNamed:@"64px.png"].CGImage; } else { [picker.navigationBar setTintColor:[UIColor colorWithRed:208.0/255.0 green:75.0/255.0 blue:109.0/255.0 alpha:1.0]]; //给navigationBar设置背景图片 if ([picker.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)]) { [picker.navigationBar setBackgroundImage:[UIImage imageNamed:@"a_nav_bg.png"] forBarMetrics:UIBarMetricsDefault]; } } picker.sourceType = UIImagePickerControllerSourceTypeCamera; picker.delegate=self; if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { [picker setModalPresentationStyle:UIModalPresentationFormSheet]; } [self presentModalViewController:picker animated:YES]; if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { [picker.view.superview setFrame:CGRectMake(113, 175, picker.view.frame.size.width, 680)]; } [picker release]; } } } } 3:第三步: 实现UIImagePickerControllerDelegate的委托方法 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo __OSX_AVAILABLE_BUT_DEPRECATED(__MAC_NA,__MAC_NA,__IPHONE_2_0,__IPHONE_3_0); - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker; - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)selectedImage editingInfo:(NSDictionary *)editingInfo { NSString *imagePath = [NSString stringWithFormat:@"%@/%@.jpg", self.documentsPath, contactsAdd.Photo]; NSError *error = nil; if([fileManager fileExistsAtPath:imagePath]) { [fileManager removeItemAtPath:imagePath error:&error]; } contactsAdd.Photo = nil; CFUUIDRef theUUID = CFUUIDCreate(NULL); CFStringRef string = CFUUIDCreateString(NULL, theUUID); CFRelease(theUUID); imageName = (NSString *)string; //将图层的边框设置为圆脚 headImageView.layer.cornerRadius = 10; headImageView.layer.masksToBounds = YES; headImageView.image = [ImageUtility imageByScalingAndCroppingForSize:selectedImage withTargetSize:CGSizeMake(130,130)]; headNewImageData= [NSData dataWithData:UIImageJPEGRepresentation(headImageView.image, 1.f)]; [headNewImageData retain]; [self dismissModalViewControllerAnimated:YES]; } - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { [self dismissModalViewControllerAnimated:YES]; }