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 ...
随机推荐
- 使用dom4j解析XML
jar包:dom4j //使用dom4j解析返回的xml SAXReader reader = new SAXReader(); Document doc = reader.read(new Byte ...
- C#学习网站记录
C# 编程指南--Microfsoft官方C#编程指南 https://msdn.microsoft.com/zh-cn/library/67ef8sbd(v=vs.100).aspx
- 自己用C语言写PIC32 serial bootloader
了解更多关于bootloader 的C语言实现,请加我QQ: 1273623966 (验证信息请填 bootloader),欢迎咨询或定制bootloader(在线升级程序). 从15年12月份以来我 ...
- sql 时间(datetime)计算
SELECT *FROM sc_sowu_orderreturnWHERE STATUS = '0'AND submit_time < DATE_ADD(now(), INTERVAL - 4 ...
- java网络流传输,中文乱码问题。
最近需要从某个网页上抓取数据.一波三折. 1. 先要找到网站页面调用后台数据服务的url地址,但是本人对js不了解,花了不少时间在分析其网页源代码的js部分,试图寻找出调用数据的链接. 后来得知浏览器 ...
- Windows性能优化关键点-Windows Performance tuning important settings
最近重装了windows8系统,发现性能差得很,远不如官方说的比win7好很多的说法.经过几个关键配置的调整,终于找回电脑原来的风采. 下面总结一下,希望对大家有帮助: 1. 检查windows服务, ...
- 在Windows和Linux上安装paramiko模块以及easy_install的安装方法
一.paramiko模块有什么用? paramiko是用python语言写的一个模块,遵循SSH2协议,支持以加密和认证的方式,进行远程服务器的连接.由于使用的是python这样的能够跨平台运行的语言 ...
- jdbk应用实例
首先要在数据库中建好表,表的属性要跟代码中的一致 使用jdbk连接数据库,并且进行增删改查的操作(curd). package com.beiwi; import java.sql.Connectio ...
- 锋利的js之验证身份证号
我们在做互联网网站时,注册个人资料时,经常要用到身份证号,我们需要对身份证进验证,不然别人随便输个号码就通过,让你感觉这个网站做得很shit. 身份证号是有规则的. 结构和形式 1.号码的结构 公民 ...
- C# 自定义特性
http://www.cnblogs.com/tekkaman/p/3983360.html#undefined https://msdn.microsoft.com/zh-cn/library/sw ...