iOS 对iphone和 ipad的摄像头和图片库的区别处理代码

2024-03-13 08:48

本文主要是介绍iOS 对iphone和 ipad的摄像头和图片库的区别处理代码,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

     iPhone跟 iPad对摄像头和图片库的代码处理有点不一样,iPad主要是用使用

UIPopoverController来包含 UIImagePickerController.


   主要的代码如下:

   1. 类从UIViewController继承,然后里面实现  UINavigationControllerDelegate跟 

UIImagePickerControllerDelegate。


   2.  将ios的appDelegate类  的laungh函数中,将相应的代码修改为:

    // Set RootViewController to windowif ( [[UIDevice currentDevice].systemVersion floatValue] < 6.0){// warning: addSubView doesn't work on iOS6[window setRootViewController:viewController];[window addSubview: viewController.view];}else{// use this method on ios6[window setRootViewController:viewController];}

    主要目的是 当SDK< 6.0的时候,也设置  
[window setRootViewController:viewController];
   防止在后面取window.rootViewController的时候crash.



   3. *.h文件里面包含这么几个成员变量:

    

    UIImagePickerController* picker_camera_;UIImagePickerController* picker_library_;UIWindow * window;UIPopoverController  * pc_image_picker_;

   4. *.mm文件的内容如下:

@implementation IPHONEAvatarstatic IPHONEAvatar* _sharedIPHONEAvatar = nil;
static int _g_iImgIndex = 0;+(IPHONEAvatar*)sharedIPHONEAvatar {@synchronized([IPHONEAvatar class]){if(!_sharedIPHONEAvatar){[[self alloc] init];}return _sharedIPHONEAvatar;}return nil;
}+(id)alloc
{@synchronized ([IPHONEAvatar class]){NSAssert(_sharedIPHONEAvatar == nil,@"Attempted to allocated a second instance of the IPHONEAvatar singleton");_sharedIPHONEAvatar = [super alloc];return _sharedIPHONEAvatar;}return nil;
}- (void)imagePickerController:(UIImagePickerController*)picker
didFinishPickingImage:(UIImage*)image
editingInfo:(NSDictionary*)editingInfo
{NSMutableDictionary * dict= [NSMutableDictionary dictionaryWithDictionary:editingInfo];[dict setObject:image forKey:@"UIImagePickerControllerEditedImage"];[self imagePickerController:picker didFinishPickingMediaWithInfo:dict];
}-(void)image:(UIImage*)image didFinishSavingWithError:(NSError*)error contextInfo:(void*)contextInfo
{UNUSED(image);UNUSED(contextInfo);if (error) {CCLOG("saving pic error!");}
}- (void)imagePickerControllerDidCancel:(UIImagePickerController*)picker
{[picker.view removeFromSuperview];[picker dismissModalViewControllerAnimated:YES];[picker release];picker = nil;picker_camera_ = nil;picker_library_ = nil;if(window){[window removeFromSuperview];[window release];window = nil;}#ifdef   HD_EDITIONif (pc_image_picker_){[pc_image_picker_  dismissPopoverAnimated:YES];pc_image_picker_ = nil;}
#else#endif}//3.x  用户选中图片后的回调
- (void)imagePickerController:(UIImagePickerController*)picker
didFinishPickingMediaWithInfo:(NSDictionary*)info
{CCLOG("imagePickerController:");if (picker == picker_camera_) {//如果是 来自照相机的image,那么先保存UIImage* original_image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];UIImageWriteToSavedPhotosAlbum(original_image, self,@selector(image:didFinishSavingWithError:contextInfo:),nil);}UIImage* image = [info objectForKey: @"UIImagePickerControllerEditedImage"];NSError** error = 0;NSFileManager* fm = [NSFileManager defaultManager];NSString* my_avatar_temp_path = [NSString stringWithFormat:@"%@/Documents/MYAVATAR_TEMP%d.PNG", NSHomeDirectory(),_g_iImgIndex++];if ([fm fileExistsAtPath:my_avatar_temp_path]) {[fm removeItemAtPath:my_avatar_temp_path error:error];}//UIImage* avatar_image = [ImageHelper image:image fillView:avatar_];UIImage* avatar_image= nil;
#ifdef   HD_EDITIONavatar_image = [ImageHelper image:image fillSize:CGSizeMake(120.0f, 120.0f)];
#elseavatar_image = [ImageHelper image:image fillSize:CGSizeMake(120.0f, 120.0f)];
#endifNSData* imageData = UIImagePNGRepresentation(avatar_image);BOOL avatar_updated = NO;if (imageData) {BOOL bSuccess = [imageData writeToFile:my_avatar_temp_path atomically:YES];if (bSuccess){CCString strAvatarPath;strAvatarPath.initWithFormat("%s",[my_avatar_temp_path UTF8String]);CCLog("strAvatarPath: %s",strAvatarPath.getCString());CCNotificationCenter::sharedNotificationCenter()->postNotification(AVATARCHANGE,&strAvatarPath);}else{CCLog("Failed to save picture");}avatar_updated = bSuccess;}[picker.view removeFromSuperview];[picker dismissModalViewControllerAnimated:YES];[picker release];picker = nil;picker_camera_ = nil;picker_library_ = nil;if (window){[window removeFromSuperview];[window release];window = nil;}#ifdef   HD_EDITIONif (pc_image_picker_){[pc_image_picker_  dismissPopoverAnimated:YES];pc_image_picker_ = nil;}
#else#endif// upload new avatar to serverif (avatar_updated){std::string avatar_hash = getImageFileHashValue([my_avatar_temp_path UTF8String]) + ".png";NSString* my_avatar_path = [NSString stringWithFormat:@"%@/Documents/%s", NSHomeDirectory(), avatar_hash.c_str()];if ([fm fileExistsAtPath:my_avatar_path]){[fm removeItemAtPath:my_avatar_path error:error];}[fm moveItemAtPath: my_avatar_temp_pathtoPath: my_avatar_patherror: error];CCString strFileName;strFileName.initWithFormat("%s",[my_avatar_path UTF8String]);std::string   strUploading = LanguageManager::sharedLanguageManager()->getLocalizedString("uploading avatar,please waiting...");UIAlertView *    baseAlert = [[[UIAlertView alloc] initWithTitle:[NSString  stringWithUTF8String:strUploading.c_str()] message:nildelegate:self cancelButtonTitle:nil otherButtonTitles:nil] autorelease];[baseAlert show];std::string  strOk =  LanguageManager::sharedLanguageManager()->getLocalizedString("OK");if (AvatarManager::sharedAvatarManager()->uploadFile(strFileName) ){[baseAlert   dismissWithClickedButtonIndex:0 animated:YES];std::string   strUploadSuccess = LanguageManager::sharedLanguageManager()->getLocalizedString("upload avatar  success!");UIAlertView* av = [[[UIAlertView alloc] initWithTitle:@"tip" message:[NSString  stringWithUTF8String:strUploadSuccess.c_str()] delegate:self cancelButtonTitle:[NSString stringWithUTF8String:strOk.c_str()] otherButtonTitles:nil] autorelease];[av show];}else{[baseAlert   dismissWithClickedButtonIndex:0 animated:YES];std::string   strUploadFailed = LanguageManager::sharedLanguageManager()->getLocalizedString("upload avatar  failed!");UIAlertView* av = [[[UIAlertView alloc] initWithTitle:@"tip" message:[NSString  stringWithUTF8String:strUploadFailed.c_str()] delegate:self cancelButtonTitle: [NSString stringWithUTF8String:strOk.c_str()] otherButtonTitles:nil] autorelease];[av show];AnalyticX::flurryLogError(FLURRY_UPLOAD_AVATAR, "UPLOAD_AVATAR_FAILED");}}}-(void)onIPHONECamera
{if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {picker_camera_ = [[UIImagePickerController alloc] init];
#ifdef   HD_EDITIONpicker_camera_.sourceType = UIImagePickerControllerSourceTypeCamera;if ([UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront]){picker_camera_.cameraDevice = UIImagePickerControllerCameraDeviceFront;}else{picker_camera_.cameraDevice = UIImagePickerControllerCameraDeviceRear;}picker_camera_.allowsEditing = YES;picker_camera_.delegate = self;#elsepicker_camera_.sourceType = UIImagePickerControllerSourceTypeCamera;if ([UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront]){picker_camera_.cameraDevice = UIImagePickerControllerCameraDeviceFront;}else{picker_camera_.cameraDevice = UIImagePickerControllerCameraDeviceRear;}picker_camera_.allowsEditing = YES;picker_camera_.delegate = self;#endif     #ifdef   HD_EDITIONpicker_camera_.view.frame = [UIScreen mainScreen].bounds;window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]];window.rootViewController = picker_camera_;//self;if ( [[UIDevice currentDevice].systemVersion floatValue] < 6.0){// warning: addSubView doesn't work on iOS6[window addSubview: picker_camera_.view];}else{// use this method on ios6[window setRootViewController:picker_camera_];//self];}[window makeKeyAndVisible];
#elsepicker_camera_.view.frame = [UIScreen mainScreen].bounds;window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]];window.rootViewController = picker_camera_;//self;if ( [[UIDevice currentDevice].systemVersion floatValue] < 6.0){// warning: addSubView doesn't work on iOS6[window addSubview: picker_camera_.view];}else{// use this method on ios6[window setRootViewController:picker_camera_];//self];}[window makeKeyAndVisible];
#endif}else{//提示摄像头无法用}
}-(void)onIPHONEAlbum
{picker_library_ = [[UIImagePickerController alloc] init];
#ifdef HD_EDITIONpicker_library_.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;picker_library_.allowsEditing = YES;picker_library_.delegate = self;#elsepicker_library_.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;picker_library_.allowsEditing = YES;picker_library_.delegate = self;#endif #ifdef HD_EDITIONCGSize  libSize  =  picker_library_.view.frame.size;//libSize = CGSizeMake(160/2, 160/2);pc_image_picker_ = [[UIPopoverController alloc] initWithContentViewController:picker_library_];if ( [[UIDevice currentDevice].systemVersion floatValue] < 6.0){CCLog("systemVersion<6.0");}else{CCLog("systemVersion>=6.0");}[pc_image_picker_ presentPopoverFromRect:CGRectMake(57/2, 352/2, libSize.width, libSize.height) inView:[[UIApplication sharedApplication] keyWindow].rootViewController.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];#else[[[UIApplication sharedApplication] keyWindow] addSubview:picker_library_.view];
#endif}@end

具体细节就不说了,主要是区分iPhone和iPad,然后区分 SDK6.0以上跟 6.0以下。


这篇关于iOS 对iphone和 ipad的摄像头和图片库的区别处理代码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/804293

相关文章

mysql外键创建不成功/失效如何处理

《mysql外键创建不成功/失效如何处理》文章介绍了在MySQL5.5.40版本中,创建带有外键约束的`stu`和`grade`表时遇到的问题,发现`grade`表的`id`字段没有随着`studen... 当前mysql版本:SELECT VERSION();结果为:5.5.40。在复习mysql外键约

2.1/5.1和7.1声道系统有什么区别? 音频声道的专业知识科普

《2.1/5.1和7.1声道系统有什么区别?音频声道的专业知识科普》当设置环绕声系统时,会遇到2.1、5.1、7.1、7.1.2、9.1等数字,当一遍又一遍地看到它们时,可能想知道它们是什... 想要把智能电视自带的音响升级成专业级的家庭影院系统吗?那么你将面临一个重要的选择——使用 2.1、5.1 还是

python实现pdf转word和excel的示例代码

《python实现pdf转word和excel的示例代码》本文主要介绍了python实现pdf转word和excel的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价... 目录一、引言二、python编程1,PDF转Word2,PDF转Excel三、前端页面效果展示总结一

在MyBatis的XML映射文件中<trim>元素所有场景下的完整使用示例代码

《在MyBatis的XML映射文件中<trim>元素所有场景下的完整使用示例代码》在MyBatis的XML映射文件中,trim元素用于动态添加SQL语句的一部分,处理前缀、后缀及多余的逗号或连接符,示... 在MyBATis的XML映射文件中,<trim>元素用于动态地添加SQL语句的一部分,例如SET或W

Python中@classmethod和@staticmethod的区别

《Python中@classmethod和@staticmethod的区别》本文主要介绍了Python中@classmethod和@staticmethod的区别,文中通过示例代码介绍的非常详细,对大... 目录1.@classmethod2.@staticmethod3.例子1.@classmethod

使用C#代码计算数学表达式实例

《使用C#代码计算数学表达式实例》这段文字主要讲述了如何使用C#语言来计算数学表达式,该程序通过使用Dictionary保存变量,定义了运算符优先级,并实现了EvaluateExpression方法来... 目录C#代码计算数学表达式该方法很长,因此我将分段描述下面的代码片段显示了下一步以下代码显示该方法如

Go语言使用Buffer实现高性能处理字节和字符

《Go语言使用Buffer实现高性能处理字节和字符》在Go中,bytes.Buffer是一个非常高效的类型,用于处理字节数据的读写操作,本文将详细介绍一下如何使用Buffer实现高性能处理字节和... 目录1. bytes.Buffer 的基本用法1.1. 创建和初始化 Buffer1.2. 使用 Writ

Python视频处理库VidGear使用小结

《Python视频处理库VidGear使用小结》VidGear是一个高性能的Python视频处理库,本文主要介绍了Python视频处理库VidGear使用小结,文中通过示例代码介绍的非常详细,对大家的... 目录一、VidGear的安装二、VidGear的主要功能三、VidGear的使用示例四、VidGea

Python结合requests和Cheerio处理网页内容的操作步骤

《Python结合requests和Cheerio处理网页内容的操作步骤》Python因其简洁明了的语法和强大的库支持,成为了编写爬虫程序的首选语言之一,requests库是Python中用于发送HT... 目录一、前言二、环境搭建三、requests库的基本使用四、Cheerio库的基本使用五、结合req

使用Python处理CSV和Excel文件的操作方法

《使用Python处理CSV和Excel文件的操作方法》在数据分析、自动化和日常开发中,CSV和Excel文件是非常常见的数据存储格式,ython提供了强大的工具来读取、编辑和保存这两种文件,满足从基... 目录1. CSV 文件概述和处理方法1.1 CSV 文件格式的基本介绍1.2 使用 python 内