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弹出框 弹出三个按钮,共用户选择
:第一步:
//取得设备型号
#import <sys/types.h>
#import <sys/sysctl.h> - (NSString *) platform{
size_t size;
sysctlbyname("hw.machine", NULL, &size, NULL, );
char *machine = malloc(size);
sysctlbyname("hw.machine", machine, &size, NULL, );
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;
} :第二步:
实现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 == )
{
return;
}
//删除
if(buttonIndex==)
{
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 == )
{
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(, , , )];
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(, , picker.view.frame.size.width, )];
}
[picker release];
}
//取像
if(buttonIndex == )
{
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(, , , )];
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(, , picker.view.frame.size.width, )];
}
[picker release];
}
}
}
}
:第三步:
实现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 = ;
headImageView.layer.masksToBounds = YES;
headImageView.image = [ImageUtility imageByScalingAndCroppingForSize:selectedImage withTargetSize:CGSizeMake(,)];
headNewImageData= [NSData dataWithData:UIImageJPEGRepresentation(headImageView.image, .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 ;媒体的URLNSString *const UIImagePickerControllerReferenceURL ;原件的URLNSString *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; |
UIImagePickerControllerDelegate---ActionSheet---获得设备型号的更多相关文章
- 【代码笔记】iOS-获得设备型号
一,代码. - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. ...
- 获取iOS设备型号iphone ipad
#import <sys/sysctl.h> //获得设备型号 -(NSString *)getCurrentDeviceModel { int mib[2]; size_t len; c ...
- iOS获取设备型号、装置类型等信息
iOS获取设备型号.设备类型等信息 设备标识 关于设备标识,历史上盛行过很多英雄,比如UDID.Mac地址.OpenUDID等,然而他们都陆陆续续倒在了苹果的门下.苹果目前提供了2个方法供App获取设 ...
- IOS 获取最新设备型号方法
1.IOS 获取最新设备型号方法列表最新对照表:http://theiphonewiki.com/wiki/Models方法: #import "sys/utsname.h” struct ...
- iOS获取设备型号
导入头文件 #include <sys/types.h> #include <sys/sysctl.h> 直接调用 //获得设备型号 + (NSString *)getCurr ...
- soliworks三维机柜布局(一)创建设备型号库
以某直升机电气系统为例:为电路中的各个设备创建设备型号库是进行三维线束设计的前提之一(如下图所示:窗口中箭头所指的3D部件一定要为每个设备都添加) 设备只有添加了3d模型,在solidworks进行机 ...
- 获取iOS设备型号的方法总结
三种常用的办法获取iOS设备的型号: 1. [UIDevice currentDevice].model (推荐): 2. uname(struct utsname *name) ,使用此函数需要#i ...
- 获得设备型号(含iPhone6 , iPhone 6+)
//获得设备型号 + (NSString *)getCurrentDeviceModel:(UIViewController *)controller { int mib[2]; size_t len ...
- iOS 获取设备型号以及IP地址
首先导入四个头文件 #include <sys/types.h> #include <sys/sysctl.h> #include <ifaddrs.h> #inc ...
随机推荐
- nvm
nvm install stable #安装最新稳定版 node,现在是 5.0.0 nvm install 4.2.2 #安装 4.2.2 版本 nvm install 0.12.7 #安装 0.1 ...
- NHibernate系列文章二十四:NHibernate查询之Linq查询(附程序下载)
摘要 NHibernate从3.0开始支持Linq查询.写Linq to NHibernate查询就跟写.net linq代码一样,非常灵活,可以很容易实现复杂的查询.这篇文章使用Linq to NH ...
- IIS7 IIS8 中多个版本php共存的方法
原文地址: https://blog.cozof.com/pieces/54.shtml 最近又重回.net,用回IIS.然后用到某个php开源项目,需要低版本的php,之前装的一个php5.5不能用 ...
- java下的字符流
输入流和输出流相对于内存设备而言.将外设中的数据读取到内存中:输入将内存的数写入到外设中:输出.字符流的由来:其实就是:字节流读取文字字节数据后,不直接操作而是先查指定的编码表.获取对应的文字.比如, ...
- $.getJSON在IE8下失效
$.getJSON("/Home/GetData?r=" + Math.random(), { ids: ids }, function(data) { //处理逻辑 }); 原因 ...
- 改变BootStrap主题颜色
摘自:http://www.asp.net/visual-studio/overview/2013/creating-web-projects-in-visual-studio#bootstrap Y ...
- cx_Oracle 报错 Reason: image not found
(Study_env) ➜ DAL python -c "import cx_Oracle"Traceback (most recent call last): File &quo ...
- java8中map的meger方法的使用
java8中map有一个merge方法使用示例: /** * 打印出包含号码集的label的集合 * * @param args */ public static void main(String[] ...
- ffmpeg基础
背景知识ffmpeg是一款领先的流媒体处理框架,支持编码,解码,转码等功能并可以在linux, Mac OS X, Microsoft Windows编译运行,用它做播放器的有:ffplay,射手播放 ...
- NodeJS 错误处理最佳实践
NodeJS的错误处理让人痛苦,在很长的一段时间里,大量的错误被放任不管.但是要想建立一个健壮的Node.js程序就必须正确的处理这些错误,而且这并不难学.如果你实在没有耐心,那就直接绕过长篇大论跳到 ...