UIImagePickerControllerDelegate---ActionSheet---获得设备型号

<pre name="code" class="java">//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];  
} 

 

1
2
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary
 *)info;

当用户选取完成后调用;

1
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker;

当用户取消选取时调用;

1
2
- (void)imagePickerController:(UIImagePickerController *)picker
 didFinishPickingMediaWithInfo:(NSDictionary *)info;

选取的信息都在info中,info 是一个字典。

字典中的键:

1
2
3
4
5
6
7
NSString *const  UIImagePickerControllerMediaType ;指定用户选择的媒体类型(文章最后进行扩展)
NSString *const  UIImagePickerControllerOriginalImage ;原始图片
NSString *const  UIImagePickerControllerEditedImage ;修改后的图片
NSString *const  UIImagePickerControllerCropRect ;裁剪尺寸
NSString *const  UIImagePickerControllerMediaURL ;媒体的URL
NSString *const  UIImagePickerControllerReferenceURL ;原件的URL
NSString *const  UIImagePickerControllerMediaMetadata;当来数据来源是照相机的时候这个值才有效

UIImagePickerController 的更多参数参考这里

代理中的功能参考这里

UIImagePickerControllerMediaType 包含着KUTTypeImage 和KUTTypeMovie

KUTTypeImage 包含:

1
2
3
4
5
6
7
8
9
10
11
const CFStringRef  kUTTypeImage ;抽象的图片类型
const CFStringRef  kUTTypeJPEG ;
const CFStringRef  kUTTypeJPEG2000 ;
const CFStringRef  kUTTypeTIFF ;
const CFStringRef  kUTTypePICT ;
const CFStringRef  kUTTypeGIF ;
const CFStringRef  kUTTypePNG ;
const CFStringRef  kUTTypeQuickTimeImage ;
const CFStringRef  kUTTypeAppleICNS 
const CFStringRef kUTTypeBMP;
const CFStringRef  kUTTypeICO;

KUTTypeMovie 包含:

1
2
3
4
5
6
7
8
9
10
const CFStringRef  kUTTypeAudiovisualContent ;抽象的声音视频
const CFStringRef  kUTTypeMovie ;抽象的媒体格式(声音和视频)
const CFStringRef  kUTTypeVideo ;只有视频没有声音
const CFStringRef  kUTTypeAudio ;只有声音没有视频
const CFStringRef  kUTTypeQuickTimeMovie ;
const CFStringRef  kUTTypeMPEG ;
const CFStringRef  kUTTypeMPEG4 ;
const CFStringRef  kUTTypeMP3 ;
const CFStringRef  kUTTypeMPEG4Audio ;
const CFStringRef  kUTTypeAppleProtectedMPEG4Audio;
posted @ 2017-01-03 11:50  超神船长  阅读(1021)  评论(0编辑  收藏  举报