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

相关文章

C#代码实现解析WTGPS和BD数据

《C#代码实现解析WTGPS和BD数据》在现代的导航与定位应用中,准确解析GPS和北斗(BD)等卫星定位数据至关重要,本文将使用C#语言实现解析WTGPS和BD数据,需要的可以了解下... 目录一、代码结构概览1. 核心解析方法2. 位置信息解析3. 经纬度转换方法4. 日期和时间戳解析5. 辅助方法二、L

Python使用Code2flow将代码转化为流程图的操作教程

《Python使用Code2flow将代码转化为流程图的操作教程》Code2flow是一款开源工具,能够将代码自动转换为流程图,该工具对于代码审查、调试和理解大型代码库非常有用,在这篇博客中,我们将深... 目录引言1nVflRA、为什么选择 Code2flow?2、安装 Code2flow3、基本功能演示

IIS 7.0 及更高版本中的 FTP 状态代码

《IIS7.0及更高版本中的FTP状态代码》本文介绍IIS7.0中的FTP状态代码,方便大家在使用iis中发现ftp的问题... 简介尝试使用 FTP 访问运行 Internet Information Services (IIS) 7.0 或更高版本的服务器上的内容时,IIS 将返回指示响应状态的数字代

C/C++和OpenCV实现调用摄像头

《C/C++和OpenCV实现调用摄像头》本文主要介绍了C/C++和OpenCV实现调用摄像头,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录准备工作1. 打开摄像头2. 读取视频帧3. 显示视频帧4. 释放资源5. 获取和设置摄像头属性

MySQL 添加索引5种方式示例详解(实用sql代码)

《MySQL添加索引5种方式示例详解(实用sql代码)》在MySQL数据库中添加索引可以帮助提高查询性能,尤其是在数据量大的表中,下面给大家分享MySQL添加索引5种方式示例详解(实用sql代码),... 在mysql数据库中添加索引可以帮助提高查询性能,尤其是在数据量大的表中。索引可以在创建表时定义,也可

使用C#删除Excel表格中的重复行数据的代码详解

《使用C#删除Excel表格中的重复行数据的代码详解》重复行是指在Excel表格中完全相同的多行数据,删除这些重复行至关重要,因为它们不仅会干扰数据分析,还可能导致错误的决策和结论,所以本文给大家介绍... 目录简介使用工具C# 删除Excel工作表中的重复行语法工作原理实现代码C# 删除指定Excel单元

Python实现一键PDF转Word(附完整代码及详细步骤)

《Python实现一键PDF转Word(附完整代码及详细步骤)》pdf2docx是一个基于Python的第三方库,专门用于将PDF文件转换为可编辑的Word文档,下面我们就来看看如何通过pdf2doc... 目录引言:为什么需要PDF转Word一、pdf2docx介绍1. pdf2docx 是什么2. by

python web 开发之Flask中间件与请求处理钩子的最佳实践

《pythonweb开发之Flask中间件与请求处理钩子的最佳实践》Flask作为轻量级Web框架,提供了灵活的请求处理机制,中间件和请求钩子允许开发者在请求处理的不同阶段插入自定义逻辑,实现诸如... 目录Flask中间件与请求处理钩子完全指南1. 引言2. 请求处理生命周期概述3. 请求钩子详解3.1

Linux中的more 和 less区别对比分析

《Linux中的more和less区别对比分析》在Linux/Unix系统中,more和less都是用于分页查看文本文件的命令,但less是more的增强版,功能更强大,:本文主要介绍Linu... 目录1. 基础功能对比2. 常用操作对比less 的操作3. 实际使用示例4. 为什么推荐 less?5.

Spring Security介绍及配置实现代码

《SpringSecurity介绍及配置实现代码》SpringSecurity是一个功能强大的Java安全框架,它提供了全面的安全认证(Authentication)和授权(Authorizatio... 目录简介Spring Security配置配置实现代码简介Spring Security是一个功能强