Unity tune IOS album

Relatively speaking, the realization of IOS is much simpler than Android, but the focus is not good IOS test to macOS, to the appropriate version of xcode, suggest that you do not engage in a virtual machine, I get a couple of days, a handful of hair drag, and finally give up direct

Look for the company's IOS with mac or to see who has borrowed a virtual machine with a relatively strong, did not talk much, get down to business

Unity call IOS album only need to put in the Plugins-IOS Unity folder two text files, and then change the suffix, a change into a .m .h the same name does not matter, we are not the same suffix, a C ++ is a OC

 

 

 

.h header file is equivalent to the introduction of a direct interface to copy the code

1 #import<QuartzCore/CADisplayLink.h>
2 @interface IOSCameraController : UIViewController<UIImagePickerControllerDelegate,UINavigationControllerDelegate,UIPopoverPresentationControllerDelegate>
3 @end

.m implementation file is, at this writing function

  . 1  #import  " IOSCameraController.h " 
  2  
  . 3  @implementation IOSCameraController
   . 4 - ( void ) OpenCamera: (UIImagePickerControllerSourceType) {type
   . 5      // Create instance UIImagePickerController 
  . 6      UIImagePickerController * Picker;
   . 7      Picker = [[UIImagePickerController the alloc] the init];
   . 8      // setting agent 
  . 9      . Picker the delegate = Self;
 10      // if editing is allowed (the default is NO) 
. 11      picker.allowsEditing = YES;
 12 is     // Set photo source 
13 is      picker.sourceType = type;
 14      // display picture selection controller 
15      IF (picker.sourceType UIImagePickerControllerSourceTypePhotoLibrary && == [[currentDevice the UIDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
 16          picker.modalPresentationStyle = UIModalPresentationPopover;
 . 17          UIPopoverPresentationController the popover * = picker.popoverPresentationController;
 18 is          // picker.preferredContentSize = [The UIScreen mainScreen] .bounds.size; 
. 19          the popover. the delegate = Self;
 20 is         popover.sourceRect = CGRectMake(0, 0, 0, 0);
 21         popover.sourceView = self.view;
 22         popover.permittedArrowDirections = UIPopoverArrowDirectionAny;
 23         [self presentViewController:picker animated:YES completion:nil];
 24     } else {
 25         [self presentViewController:picker animated:YES completion:^{}];
 26     }
 27    
 28 }
 29 -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
 30     [picker dismissViewControllerAnimated:YES completion:^{}];
 31     UIImage *image = [info objectForKey:@"UIImagePickerControllerEditedImage"];
 32     if (image == nil) {
 33         image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
 34     }
 35     //图片旋转
 36     if (image.imageOrientation != UIImageOrientationUp) {
 37     //图片旋转
 38         image = [self fixOrientation:image];
 39     }
 40     NSString *imagePath = [self GetSavePath:@"Temp.jpg"];
 41     [self SaveFileToDoc:image path:imagePath];
 42 }
 43 -(NSString*)GetSavePath:(NSString *)filename{
 44     NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
 45     NSString *docPath = [pathArray objectAtIndex:0];
 46     return [docPath stringByAppendingPathComponent:filename];
 47 }
 48 -(void)SaveFileToDoc:(UIImage *)image path:(NSString *)path{
 49     NSData *data;
 50     if (UIImagePNGRepresentation(image)==nil) {
 51         data = UIImageJPEGRepresentation(image, 1);
 52     }else{
 53         data = UIImagePNGRepresentation(image);
 54     }
 55     [data writeToFile:path atomically:YES];
 56     UnitySendMessage("Canvas", " The Message " , " Temp.jpg " );
 57 is  }
 58  #pragma Mark image processing method
 59  // Image rotation process 
60 - (the UIImage *) fixOrientation: (the UIImage * ) {aImage
 61 is      CGAffineTransform Transform = CGAffineTransformIdentity;
 62 is      
63 is      Switch (aImage.imageOrientation) {
 64          Case UIImageOrientationDown:
 65          Case UIImageOrientationDownMirrored:
 66              Transform = CGAffineTransformTranslate(transform, aImage.size.width, aImage.size.height);
 67             transform = CGAffineTransformRotate(transform, M_PI);
 68             break;
 69             
 70         case UIImageOrientationLeft:
 71         case UIImageOrientationLeftMirrored:
 72             transform = CGAffineTransformTranslate(transform, aImage.size.width, 0);
 73             transform = CGAffineTransformRotate(transform, M_PI_2);
 74             break;
 75             
 76         case UIImageOrientationRight:
 77         case UIImageOrientationRightMirrored:
 78             transform = CGAffineTransformTranslate(transform, 0, aImage.size.height);
 79             transform = CGAffineTransformRotate(transform, -M_PI_2);
 80             break;
 81         default:
 82             break;
 83     }
 84     
 85     switch (aImage.imageOrientation) {
 86         case UIImageOrientationUpMirrored:
 87         case UIImageOrientationDownMirrored:
 88             transform = CGAffineTransformTranslate(transform, aImage.size.width, 0);
 89             transform = CGAffineTransformScale(transform, -1, 1);
 90             break;
 91             
 92         case UIImageOrientationLeftMirrored:
 93         case UIImageOrientationRightMirrored:
 94             transform = CGAffineTransformTranslate(transform, aImage.size.height, 0);
 95             transform = CGAffineTransformScale(transform, -1, 1);
 96             break;
 97         default:
 98             break;
 99     }
100     
101     // Now we draw the underlying CGImage into a new context, applying the transform
102     // calculated above.
103     CGContextRef ctx = CGBitmapContextCreate(NULL, aImage.size.width, aImage.size.height,
104                                              CGImageGetBitsPerComponent(aImage.CGImage), 0,
105                                              CGImageGetColorSpace(aImage.CGImage),
106                                              CGImageGetBitmapInfo(aImage.CGImage));
107     CGContextConcatCTM(ctx, transform);
108     switch (aImage.imageOrientation) {
109         case UIImageOrientationLeft:
110         case UIImageOrientationLeftMirrored:
111         case UIImageOrientationRight:
112         case UIImageOrientationRightMirrored:
113             // Grr...
114             CGContextDrawImage(ctx, CGRectMake(0,0,aImage.size.height,aImage.size.width), aImage.CGImage);
115             break;
116             
117         default:
118             CGContextDrawImage(ctx, CGRectMake(0,0,aImage.size.width,aImage.size.height), aImage.CGImage);
119             break;
120     }
121     // And now we just create a new UIImage from the drawing context
122     CGImageRef cgimg = CGBitmapContextCreateImage(ctx);
123     UIImage *img = [UIImage imageWithCGImage:cgimg];
124     CGContextRelease(ctx);
125     CGImageRelease(cgimg);
126     return img;
127 }
128 @end
129 #if defined(__cplusplus)
130 extern "C" {
131 #endif
132     void IOS_OpenCamera(){
133         IOSCameraController *app = [[IOSCameraController alloc]init];
134         UIViewController *vc = UnityGetGLViewController();
135         [vc.view addSubview:app.view];
136         [app OpenCamera:UIImagePickerControllerSourceTypeCamera];
137     }
138     void IOS_OpenAlbum(){
139         IOSCameraController *app = [[IOSCameraController alloc]init];
140         UIViewController *vc = UnityGetGLViewController();
141         [vc.view addSubview:app.view];
142         [app OpenCamera:UIImagePickerControllerSourceTypePhotoLibrary];
143     }
144 #if defined(__cplusplus)
145 }
146 #endif

IOS is so simple and crude

Then the test script, focusing on calls to see how

 1 using System.Collections;
 2 using System.Collections.Generic;
 3 using UnityEngine;
 4 using UnityEngine.UI;
 5 using System.IO;
 6 using System.Runtime.InteropServices;
 7 using UnityEngine.SceneManagement;
 8 
 9 public class TestWWWTex : MonoBehaviour {
10 
11     [SerializeField] private Button _openCamera; //打开相机按钮
12     [SerializeField] private Button _openAlbum; //Open album button 
13 is      [SerializeField] Private rawlmage _image; // picture for display
 14      // two methods defined in the introduction oc 
15      [the DllImport ( " __Internal " )]
 16      Private  static  extern  void IOS_OpenCamera ();
 . 17      [the DllImport ( " __Internal " )]
 18 is      Private  static  extern  void IOS_OpenAlbum ();
 . 19  
20 is      void the Awake ()
 21 is      {
 22 is          // add click event two Button 
23 is         _openCamera.onClick.AddListener(IOS_OpenCamera);
24         _openAlbum.onClick.AddListener(IOS_OpenAlbum);
25     }
26 
27     //ios回调unity的函数
28     void Message(string filenName)
29     {
30         31         
32         string filePath = Application.persistentDataPath + "/" + filenName;
33         34        
35         TestFileFunc(filePath);
36     }
37 
38     void TestFileFunc(string path)
39     {
40         FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read);
41         fileStream.Seek(0, SeekOrigin.Begin);
42         byte[] bye = new byte[fileStream.Length];
43         fileStream.Read(bye, 0, bye.Length);
44         fileStream.Close();
45 
46         Texture2D texture2D = new Texture2D(240, 144);
47         texture2D.LoadImage(bye);
48 
49         _image.texture = texture2D;
50 
51     }
52 }

Here to talk about, www load will not find photos, http load, xcode will complain, do not understand what went wrong newspaper, as if did not give permission, xcode recommended to use https, I would not bother to engage directly with the file stream to load it

The next step is to find info.plist playing xcode project file in xcode, the album plus a privilege, and they can test the real machine

Guess you like

Origin www.cnblogs.com/shawnlen/p/11813353.html