ios选择图片功能,结果列表扩大

This commit is contained in:
Caiyanpeng committed 2021-09-07 15:26:43 +08:00
1 parent 510cd11049
commit fe65b83c89
12 files changed
+295 -31

No files matched your search

+4 -1
View File
@@ -70,7 +70,10 @@ public static class XCodePostProcessBuild
PlistElementDict qxLoc = plist.root.CreateDict("NSLocationWhenInUseUsageDescription");
qxLoc.SetBoolean("NSLocationWhenInUseUsageDescription", true);
plist.root.SetString("NSCameraUsageDescription","cameraDesciption");
plist.root.SetString("NSContactsUsageDescription", "contactsDesciption");
plist.root.SetString("NSMicrophoneUsageDescription", "microphoneDesciption");
plist.root.SetString("NSPhotoLibraryUsageDescription", "photoLibraryDesciption");
File.WriteAllText(listPath, plist.WriteToString());
}
+5
View File
@@ -0,0 +1,5 @@
//import 引用头文件 相当于Using
#import<QuartzCore/CADisplayLink.h>
//声明一个IOSCameraController类 继承自UIViewController <>里面是是协议/代理的调用声明 可以理解为c#的接口
@interface IOSCameraController : UIViewController<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@end
@@ -0,0 +1,33 @@
fileFormatVersion: 2
guid: a11b3afe071c6854388ea27186ccaf4f
PluginImporter:
externalObjects: {}
serializedVersion: 2
iconMap: {}
executionOrder: {}
defineConstraints: []
isPreloaded: 0
isOverridable: 0
isExplicitlyReferenced: 0
validateReferences: 1
platformData:
- first:
Any:
second:
enabled: 0
settings: {}
- first:
Editor: Editor
second:
enabled: 0
settings:
DefaultValueInitialized: true
- first:
iPhone: iOS
second:
enabled: 1
settings:
AddToEmbeddedBinaries: false
userData:
assetBundleName:
assetBundleVariant:
+173
View File
@@ -0,0 +1,173 @@
#import "IOSCameraController.h"
@implementation IOSCameraController
-(void)OpenTarget:(UIImagePickerControllerSourceType)type{
//创建UIImagePickerController实例
UIImagePickerController *picker;
picker= [[UIImagePickerController alloc]init];
//设置代理
picker.delegate = self;
//是否允许编辑 (默认为NO)
picker.allowsEditing = YES;
//设置照片的来源
// UIImagePickerControllerSourceTypePhotoLibrary, // 来自图库
// UIImagePickerControllerSourceTypeCamera, // 来自相机
// UIImagePickerControllerSourceTypeSavedPhotosAlbum // 来自相册
picker.sourceType = type;
//这里需要判断设备是iphone还是ipad 如果使用的是iphone并没有问题 但是如果 是在ipad上调用相册获取图片 会出现没有确定(选择)的按钮 所以这里判断
//了一下设备,针对ipad 使用另一种方法 但是这种方法是弹出一个界面 并不是覆盖整个界面 需要改进 试过另一种方式 重写一个相册界面
//(QQ的ipad选择头像的界面 就使用了这种方式 但是这里我们先不讲 (因为我也不太懂 但是我按照简书的一位老哥的文章写出来了 这里放一下这个简书的链接
//https://www.tlbyxzcx.com)
if (picker.sourceType == UIImagePickerControllerSourceTypePhotoLibrary &&[[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
// 设置弹出的控制器的显示样式
picker.modalPresentationStyle = UIModalPresentationPopover;
//获取这个弹出控制器
UIPopoverPresentationController *popover = picker.popoverPresentationController;
//设置代理
popover.delegate = self;
//下面两个属性设置弹出位置
popover.sourceRect = CGRectMake(0, 0, 0, 0);
popover.sourceView = self.view;
//设置箭头的位置
popover.permittedArrowDirections = UIPopoverArrowDirectionAny;
//展示选取照片控制器
[self presentViewController:picker animated:YES completion:nil];
} else {
//展示选取照片控制器
[self presentViewController:picker animated:YES completion:^{}];
}
}
//选择完成,点击界面中的某个图片或者选择(Choose)按钮时触发
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
//关闭界面
[picker dismissViewControllerAnimated:YES completion:^{}];
//得到照片
UIImage *image = [info objectForKey:@"UIImagePickerControllerEditedImage"];
if (image == nil) {
image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
}
//有些时候我们拍照后经常发现导出的照片方向会有问题,要么横着,要么颠倒着,需要旋转才适合观看。但是在ios设备上是正常的
//所以在这里处理了图片 让他旋转成我们需要的
if (image.imageOrientation != UIImageOrientationUp) {
//图片旋转
image = [self fixOrientation:image];
}
//获取保存图片的地址
NSString *imagePath = [self GetSavePath:@"Temp.jpg"];
//保存图片到沙盒路径 对应unity中的Application.persistentDataPath 之后我们取图片就需要在这个路径下取 这是一个可读可写的路径
[self SaveFileToDoc:image path:imagePath];
}
//获取保存文件的路径 如果有返回路径 没有创建一个返回
-(NSString*)GetSavePath:(NSString *)filename{
NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docPath = [pathArray objectAtIndex:0];
return [docPath stringByAppendingPathComponent:filename];
}
//将图片保存到沙盒路径
-(void)SaveFileToDoc:(UIImage *)image path:(NSString *)path{
NSData *data;
if (UIImagePNGRepresentation(image)==nil) {
data = UIImageJPEGRepresentation(image, 1);
}else{
data = UIImagePNGRepresentation(image);
}
[data writeToFile:path atomically:YES];
//保存之后通知unity 执行对应的回调
//UnitySendMessage 是用来给unity发消息的 有三个参数 1.挂载对应回调脚本的物体名 2.回调函数的名称 3.对应回调上的参数
UnitySendMessage("FeedbackModal(Clone)", "OnMobileImageSelect", [NSString stringWithFormat:@"true,%@",path ].UTF8String);
}
#pragma mark 图片处理方法
//图片旋转处理
- (UIImage *)fixOrientation:(UIImage *)aImage {
CGAffineTransform transform = CGAffineTransformIdentity;
switch (aImage.imageOrientation) {
case UIImageOrientationDown:
case UIImageOrientationDownMirrored:
transform = CGAffineTransformTranslate(transform, aImage.size.width, aImage.size.height);
transform = CGAffineTransformRotate(transform, M_PI);
break;
case UIImageOrientationLeft:
case UIImageOrientationLeftMirrored:
transform = CGAffineTransformTranslate(transform, aImage.size.width, 0);
transform = CGAffineTransformRotate(transform, M_PI_2);
break;
case UIImageOrientationRight:
case UIImageOrientationRightMirrored:
transform = CGAffineTransformTranslate(transform, 0, aImage.size.height);
transform = CGAffineTransformRotate(transform, -M_PI_2);
break;
default:
break;
}
switch (aImage.imageOrientation) {
case UIImageOrientationUpMirrored:
case UIImageOrientationDownMirrored:
transform = CGAffineTransformTranslate(transform, aImage.size.width, 0);
transform = CGAffineTransformScale(transform, -1, 1);
break;
case UIImageOrientationLeftMirrored:
case UIImageOrientationRightMirrored:
transform = CGAffineTransformTranslate(transform, aImage.size.height, 0);
transform = CGAffineTransformScale(transform, -1, 1);
break;
default:
break;
}
// Now we draw the underlying CGImage into a new context, applying the transform
// calculated above.
CGContextRef ctx = CGBitmapContextCreate(NULL, aImage.size.width, aImage.size.height,
CGImageGetBitsPerComponent(aImage.CGImage), 0,
CGImageGetColorSpace(aImage.CGImage),
CGImageGetBitmapInfo(aImage.CGImage));
CGContextConcatCTM(ctx, transform);
switch (aImage.imageOrientation) {
case UIImageOrientationLeft:
case UIImageOrientationLeftMirrored:
case UIImageOrientationRight:
case UIImageOrientationRightMirrored:
// Grr...
CGContextDrawImage(ctx, CGRectMake(0,0,aImage.size.height,aImage.size.width), aImage.CGImage);
break;
default:
CGContextDrawImage(ctx, CGRectMake(0,0,aImage.size.width,aImage.size.height), aImage.CGImage);
break;
}
// And now we just create a new UIImage from the drawing context
CGImageRef cgimg = CGBitmapContextCreateImage(ctx);
UIImage *img = [UIImage imageWithCGImage:cgimg];
CGContextRelease(ctx);
CGImageRelease(cgimg);
return img;
}
@end
//由于C++编译器需要支持函数的重载,会改变函数的名称,因此dll的导出函数通常是标准C定义的。
//这就使得C和C++的互相调用变得很常见。但是有时可能又会直接用C来调用,不想重新写代码,
//让标准C编写的dll函数定义在C和C++编译器下都能编译通过,通常会使用以下的格式:(这个格式在很多成熟的代码中很常见)
#if defined(__cplusplus)
extern "C" {
#endif
//导出接口供unity使用
void IOS_OpenCamera(){
IOSCameraController *app = [[IOSCameraController alloc]init];
UIViewController *vc = UnityGetGLViewController();
[vc.view addSubview:app.view];
[app OpenTarget:UIImagePickerControllerSourceTypeCamera];
}
void IOS_OpenAlbum(){
IOSCameraController *app = [[IOSCameraController alloc]init];
UIViewController *vc = UnityGetGLViewController();
[vc.view addSubview:app.view];
[app OpenTarget:UIImagePickerControllerSourceTypePhotoLibrary];
}
#if defined(__cplusplus)
}
#endif
@@ -0,0 +1,33 @@
fileFormatVersion: 2
guid: abaf756c4b6577148bdfc4e32d4e7a8e
PluginImporter:
externalObjects: {}
serializedVersion: 2
iconMap: {}
executionOrder: {}
defineConstraints: []
isPreloaded: 0
isOverridable: 0
isExplicitlyReferenced: 0
validateReferences: 1
platformData:
- first:
Any:
second:
enabled: 0
settings: {}
- first:
Editor: Editor
second:
enabled: 0
settings:
DefaultValueInitialized: true
- first:
iPhone: iOS
second:
enabled: 1
settings:
AddToEmbeddedBinaries: false
userData:
assetBundleName:
assetBundleVariant:
@@ -120,10 +120,10 @@ RectTransform:
m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0.5}
m_AnchorMax: {x: 1, y: 0.5}
m_AnchorMin: {x: 0.5, y: 0.5}
m_AnchorMax: {x: 0.5, y: 0.5}
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 0, y: 390}
m_SizeDelta: {x: 844, y: 390}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!222 &465239219729653175
CanvasRenderer:
@@ -249,9 +249,9 @@ RectTransform:
m_Father: {fileID: 3070817481054122626}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 1}
m_AnchorMax: {x: 0, y: 1}
m_AnchoredPosition: {x: 30, y: -5}
m_AnchorMin: {x: 0, y: 0.5}
m_AnchorMax: {x: 0, y: 0.5}
m_AnchoredPosition: {x: 30, y: 190}
m_SizeDelta: {x: 0, y: 52}
m_Pivot: {x: 0, y: 1}
--- !u!222 &3070817480521919872
@@ -512,10 +512,10 @@ RectTransform:
m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0.5, y: 0.5}
m_AnchorMax: {x: 0.5, y: 0.5}
m_AnchorMin: {x: 0.5, y: 0}
m_AnchorMax: {x: 0.5, y: 1}
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 844, y: 390}
m_SizeDelta: {x: 844, y: 0}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!222 &3070817481054122627
CanvasRenderer:
@@ -1361,9 +1361,9 @@ RectTransform:
m_Father: {fileID: 5897250536070262098}
m_RootOrder: 1
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 1}
m_AnchorMax: {x: 0, y: 1}
m_AnchoredPosition: {x: -20, y: -140}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 0, y: 0}
m_AnchoredPosition: {x: -20, y: 245}
m_SizeDelta: {x: 90, y: 1}
m_Pivot: {x: 1, y: 1}
--- !u!222 &4134945106620816021
@@ -1499,7 +1499,7 @@ RectTransform:
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 0, y: 0}
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 674, y: 385}
m_SizeDelta: {x: 674, y: 0}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!222 &4915624194418938829
CanvasRenderer:
@@ -1575,7 +1575,7 @@ RectTransform:
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 0, y: 0}
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 674, y: 385}
m_SizeDelta: {x: 674, y: 0}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!222 &4915624194703378359
CanvasRenderer:
@@ -2087,9 +2087,9 @@ RectTransform:
m_Father: {fileID: 5897250536070262098}
m_RootOrder: 2
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 1}
m_AnchorMax: {x: 0, y: 1}
m_AnchoredPosition: {x: -132, y: -151}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 0, y: 0}
m_AnchoredPosition: {x: -132, y: 234}
m_SizeDelta: {x: 134, y: 28}
m_Pivot: {x: 0, y: 1}
--- !u!222 &5897250536639853673
@@ -2340,7 +2340,7 @@ MonoBehaviour:
m_ChildForceExpandWidth: 1
m_ChildForceExpandHeight: 1
m_ChildControlWidth: 0
m_ChildControlHeight: 0
m_ChildControlHeight: 1
m_ChildScaleWidth: 0
m_ChildScaleHeight: 0
--- !u!1 &5897250537694636682
@@ -2376,9 +2376,9 @@ RectTransform:
m_Father: {fileID: 5897250536070262098}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 1}
m_AnchorMax: {x: 0, y: 1}
m_AnchoredPosition: {x: -23, y: -102}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 0, y: 0}
m_AnchoredPosition: {x: -23, y: 283}
m_SizeDelta: {x: 84, y: 28}
m_Pivot: {x: 1, y: 1}
--- !u!222 &5897250537694636678
+1 -2
View File
@@ -11,7 +11,7 @@ using Assets.Scripts.Devices;
public static class App
{
public static string Host = "http://192.168.0.102:5082/";
public static string Host = "http://192.168.0.101:5184/";
public static string AppVersion = Application.version;
@@ -85,7 +85,6 @@ public static class App
public readonly static bool isFullScreen = width/height>1.8;
static App()
{
Debug.Log(isFullScreen);
#if !UNITY_EDITOR
//Host = "http://pf.juze.pro/";
//UdpAddress = new IPEndPoint(IPAddress.Parse("47.97.84.8"), 21000);
@@ -6,16 +6,23 @@ using UnityEngine;
public class ImageSelectorController
{
private AndroidJavaObject mainActivityObject = null;
#if UNITY_IOS
[DllImport("__Internal")]
private static extern void IOS_OpenCamera();
[DllImport("__Internal")]
private static extern void IOS_OpenAlbum();
#endif
private static ImageSelectorController _instance;
public static ImageSelectorController Instance
{
get
{
#if !UNITY_EDITOR
if (_instance == null)
{
_instance = new ImageSelectorController();
}
#endif
return _instance;
}
}
@@ -30,6 +37,10 @@ public class ImageSelectorController
}
public void Select()
{
#if UNITY_IOS
IOS_OpenAlbum();
#else
mainActivityObject.Call("Select");
#endif
}
}
@@ -47,13 +47,18 @@ public class MapListController : PFUIPanel
{
#if (UNITY_ANDROID || UNITY_IOS)
topContainer = transform.Find("Top");
var rectTransform = transform.GetComponent<RectTransform>();
var offsetMax = rectTransform.offsetMax;
rectTransform.offsetMax = new Vector2(0, offsetMax.y);
var offsetMin = rectTransform.offsetMin;
rectTransform.offsetMin = new Vector2(0, offsetMin.y);
//var rectTransform = transform.GetComponent<RectTransform>();
//var offsetMax = rectTransform.offsetMax;
//rectTransform.offsetMax = new Vector2(0, offsetMax.y);
//var offsetMin = rectTransform.offsetMin;
//rectTransform.offsetMin = new Vector2(0, offsetMin.y);
var widthDelta = (transform.parent.parent.GetComponent<RectTransform>().sizeDelta.x -
transform.GetComponent<RectTransform>().sizeDelta.x)/2;
Debug.Log(widthDelta);
//筛选按钮
var topRect = topContainer.GetComponent<RectTransform>();
topRect.localPosition = new Vector3(topRect.localPosition.x - widthDelta
, topRect.localPosition.y, topRect.localPosition.z);
if (!App.topRectStartX.HasValue)
{
App.topRectStartX = topRect.localPosition.x;
@@ -15,6 +15,9 @@ public class ResultListPanelController : PFUIPanel
{
UIManager.SwitchAccount();
});
var rect = transform.GetComponent<RectTransform>();
rect.offsetMax = new Vector2(rect.offsetMax.x, 0);
rect.offsetMin = new Vector2(rect.offsetMin.x, 0);
}
public override void Show()
{
-1
View File
@@ -38,7 +38,6 @@ GraphicsSettings:
- {fileID: 16000, guid: 0000000000000000f000000000000000, type: 0}
- {fileID: 16001, guid: 0000000000000000f000000000000000, type: 0}
- {fileID: 17000, guid: 0000000000000000f000000000000000, type: 0}
- {fileID: 16003, guid: 0000000000000000f000000000000000, type: 0}
m_PreloadedShaders: []
m_SpritesDefaultMaterial: {fileID: 10754, guid: 0000000000000000f000000000000000,
type: 0}