Difference processing code of iOS for iphone and ipad cameras and image Libraries

Source: Internet
Author: User

The iPhone and iPad have different processing methods for the camera and image library code. The iPad is mainly used

UIPopoverController to include UIImagePickerController.

The main code is as follows:

1. The class is inherited from UIViewController, and then UINavigationControllerDelegate and

UIImagePickerControllerDelegate.

2. Modify the corresponding code in the laungh function of the appDelegate class of ios:

    // 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];    }

The main purpose is to set

[window setRootViewController:viewController];
Prevent crash when window. rootViewController is used later.

3. *. The H file contains the following member variables:

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

4. *. mm file content is as follows:

@ Implementation IPHONEAvatarstatic IPHONEAvatar * _ region = 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 *) pickerdidFinis HPickingImage :( UIImage *) imageeditingInfo :( NSDictionary *) editingInfo {partition * dict = [partition dictionaryWithDictionary: editingInfo]; [dict setObject: image forKey: @ "partition"]; [self imagePickerController: picker didFinishPickingMediaWithInfo: dict];}-(void) image :( UIImage *) image didFinishSavingWithError :( NSError *) error contextInfo :( void *) contextInfo {UNU SED (image); UNUSED (contextInfo); if (error) {CCLOG ("saving pic error! ") ;}}- (Void) imagePickerControllerDidCancel :( UIImagePickerController *) picker {[picker. view removeFromSuperview]; [picker preview: 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: YE S]; pc_image_picker _ = nil ;}# else # endif} // callback of the image selected by the user-(void) imagePickerController :( UIImagePickerController *) Then :( NSDictionary *) info {CCLOG ("imagePickerController:"); if (picker = picker_camera _) {// if it is an image from a camera, save UIImage * original_image = [info objectForKey: @ "UIImagePickerControllerOriginalImage"]; UIImageWriteToSavedPhotosAlbum (original_image, self, @ se Lector (image: didFinishSavingWithError: contextInfo :), nil);} UIImage * image = [info objectForKey: @ "UIImagePickerControllerEditedImage"]; NSError ** error = 0; NSFileManager * fm = [NSFileManager ultultmanager]; NSString * my_avatar_temp_path = [NSString stringWithFormat: @ "% @/Documents/MYAVATAR_TEMP % d. PNG ", NSHomeDirectory (), _ g_iImgIndex ++]; if ([fm fileExistsAtPath: my_avatar_temp_path]) {[fm removeItemA TPath: 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 (1200000f, 1200000f)]; # else percent = [ImageHelper image: image fillSize: CGSizeMake (1200000f, 1200000f)]; # endif NSData * imageData = UIImagePNGRepresentation (avatar_image); BOOL avatar_update D = 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: sharednotifcenter Center ()-> postNotification (AVATARCHANGE, & strAvatarPath);} else {CCLog ("Failed to save picture");} Vatar_updated = bSuccess;} [picker. view removeFromSuperview]; [picker preview: 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 _ blank: YES]; pc_image_picker _ = nil;} # else # endif/uploa D 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 (), outputs ()]; 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", [receivutf8string]); std: string strUploading = receivagemanager: sharedreceivagemanager ()-> getLocalizedString ("uploading avatar, plewaitase ing... "); UIAlertView * baseAlert = [[UIAlertView alloc] initWithTitle: [NSString stringwithuf8string: strUploading. c_str ()] message: nil delegate: self cancelButtonTitle: nil other ButtonTitles: nil] autorelles]; [baseAlert show]; std: string strOk = receivagemanager: sharedreceivagemanager ()-> getLocalizedString ("OK"); if (AvatarManager :: sharedAvatarManager ()-> uploadFile (strFileName) {[baseAlert dismissWithClickedButtonIndex: 0 animated: YES]; std: string strUploadSuccess = LanguageManager: sharedemediagemanager () -> getLocalizedString ("upload avatar success! "); UIAlertView * av = [[[UIAlertView alloc] initWithTitle: @" tip "message: [NSString stringwithuf8string: strUploadSuccess. c_str ()] delegate: self cancelButtonTitle: [NSString stringwithuf8string: strOk. c_str ()] otherButtonTitles: nil] autorelles]; [av show];} else {[baseAlert failed: 0 animated: YES]; std: string strUploadFailed = LanguageManager :: sharedreceivagemanager ()-> getLoc AlizedString ("upload avatar failed! "); UIAlertView * av = [[[UIAlertView alloc] initWithTitle: @" tip "message: [NSString stringwithuf8string: strUploadFailed. c_str ()] delegate: self cancelButtonTitle: [NSString stringwithuf8string: strOk. c_str ()] otherButtonTitles: nil] autorelles]; [av show]; AnalyticX: flurryLogError (response, "UPLOAD_AVATAR_FAILED") ;}}- (void) onIPHONECamera {if ([UIImagePickerController isSourceTypeAvailable: available]) {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 {// indicates that the camera cannot be used}-(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 _ presentpoverfromrect: CGRectMake (57/2, 352/2, libSize. width, libSize. height) inView: [[UIApplication sharedApplication] keyWindow]. rootViewController. view permittedArrowDirections ctions: UIPopoverArrowDirectionAny animated: YES]; # else [[[UIApplication sharedApplication] keyWindow] addSubview: picker_library _. view]; # endif} @ end

I will not talk about the specific details, mainly to distinguish between iPhone and iPad, and then to distinguish between SDK6.0 and above and below 6.0.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.