iOS 对iphone和 ipad的摄像头和图片库的区别处理代码
来源:互联网 发布:河北网络公益广告大赛 编辑:程序博客网 时间:2024/05/01 03:29
iPhone跟 iPad对摄像头和图片库的代码处理有点不一样,iPad主要是用使用
主要目的是 当SDK< 6.0的时候,也设置
具体细节就不说了,主要是区分iPhone和iPad,然后区分 SDK6.0以上跟 6.0以下。
UIPopoverController来包含 UIImagePickerController.
主要的代码如下:
1. 类从UIViewController继承,然后里面实现 UINavigationControllerDelegate跟
UIImagePickerControllerDelegate。
2. 将ios的appDelegate类 的laungh函数中,将相应的代码修改为:
// Set RootViewController to window if ( [[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*)pickerdidFinishPickingImage:(UIImage*)imageeditingInfo:(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_EDITION if (pc_image_picker_) { [pc_image_picker_ dismissPopoverAnimated:YES]; pc_image_picker_ = nil; }#else #endif }//3.x 用户选中图片后的回调- (void)imagePickerController:(UIImagePickerController*)pickerdidFinishPickingMediaWithInfo:(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_EDITION avatar_image = [ImageHelper image:image fillSize:CGSizeMake(120.0f, 120.0f)];#else avatar_image = [ImageHelper image:image fillSize:CGSizeMake(120.0f, 120.0f)];#endif NSData* 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_EDITION if (pc_image_picker_) { [pc_image_picker_ dismissPopoverAnimated:YES]; pc_image_picker_ = nil; }#else #endif // upload new avatar to server if (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_path error: 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:nil delegate: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_EDITION picker_camera_.sourceType = UIImagePickerControllerSourceTypeCamera; if ([UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront]) { picker_camera_.cameraDevice = UIImagePickerControllerCameraDeviceFront; } else { picker_camera_.cameraDevice = UIImagePickerControllerCameraDeviceRear; } picker_camera_.allowsEditing = YES; picker_camera_.delegate = self; #else picker_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_EDITION picker_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];#else picker_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_EDITION picker_library_.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; picker_library_.allowsEditing = YES; picker_library_.delegate = self; #else picker_library_.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; picker_library_.allowsEditing = YES; picker_library_.delegate = self; #endif #ifdef HD_EDITION CGSize 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的摄像头和图片库的区别处理代码
- iphone和ipad的一些区别
- iphone和iPad开发的区别
- 在iOS设备(iPhone/iPad)中调用摄像头进行全屏拍摄时的处理方案
- iOS程序开发对iPhone和iPad兼容性的规划方法
- iOS网络高级编程:iPhone和iPad的企业应用开发之错误处理
- iPhone和iPad的图标
- iPhone和iPad的图标
- [IOS开发]IPhone,IPad的icon图片大小和命名
- iOS开发iPad和iPhone的启动画面设置
- ios storyboard中 iphone 和iPad 之间的转换方式
- ios-day25-01(iPad和iPhone开发的异同)
- 【IOS】关于ipad和iPhone尺寸的分辨率
- UIImagePickerController在iPhone和iPad上的区别
- UIImagePickerController在iPhone和iPad上的区别
- 代码区分 ipad和iphone
- Iphone 和 Ipad 开发 区别
- iPhone 和 iPad的ios 开发中 利用 WebViewJavascriptBridge组件,通过 UIWebView 对Html进行双向通讯
- UVa 10474 - Where is the Marble?
- vc mfc实现dialog半透明置顶效果
- mono中的中文问题
- 黑马程序员---线程
- 杭电2016
- iOS 对iphone和 ipad的摄像头和图片库的区别处理代码
- 【VS_2012】关于缺少MSVCP100D.dll的错误
- & 和 &&、|和||的区别:
- HDU 1068最大独立集 = n - 最大匹配
- Android官方提供的支持不同屏幕大小的全部方法
- hdu 1179
- 杭电2017
- 音乐,阳光,csdn,书籍和代码
- CVE-2012-1535分析报告