Unity3d本地上传并且裁剪图片-----iOS平台

注:引擎版本unity4.x

最近项目需求,需要做用户头像,要求:

1.  可以从本地上传

2.  本地裁剪

3.  压缩控制大小

4.  在三个平台实现PC/Android/IOS

弄了好几天总是搞完了总结一下

在网上找了一段时间,有人推荐了一个插件,网盘地址忘记存了,直接上代码吧

OpenPhotoController.h :

@interface OpenPhotoController : NSObject<UIApplicationDelegate,UIImagePickerControllerDelegate, UIActionSheetDelegate,UINavigationControllerDelegate>

{

UIView*                _rootView;

UIViewController*    _rootController;

@private

id _popoverViewController;

}

@property (nonatomic, retain) id popoverViewController;

@end

OpenPhotoController.mm:

#import "OpenPhotoController.h"

@implementation OpenPhotoController
@synthesize popoverViewController = _popoverViewController;

-(void)showActionSheet
{
    UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:nil
                                                delegate:self
                                                cancelButtonTitle:NSLocalizedString( @"取消", nil )
                                                destructiveButtonTitle:nil
											    otherButtonTitles:NSLocalizedString( @"拍照", nil ), NSLocalizedString( @"相册", nil ), nil];

	if( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad )
		[sheet showFromRect:CGRectMake( 0, 0, 128, 128 ) inView:UnityGetGLViewController().view animated:YES];
	else
		[sheet showInView:UnityGetGLViewController().view];

	//[sheet release];

}
- (void)actionSheet:(UIActionSheet*)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
	if( buttonIndex == 0 )
	{
		[self showPicker:UIImagePickerControllerSourceTypeCamera];
	}
	else if( buttonIndex == 1 )
	{
		[self showPicker:UIImagePickerControllerSourceTypePhotoLibrary];
	}
	else // Cancelled
	{
		//UnityPause( false );
		//UnitySendMessage( "EtceteraManager", "imagePickerDidCancel", "" );
	}
}

- (void)showPicker:(UIImagePickerControllerSourceType)type
{
	UIImagePickerController *picker = [[UIImagePickerController alloc] init];
	picker.delegate = self;
	picker.sourceType = type;
	picker.allowsEditing = YES;

	// We need to display this in a popover on iPad
	if( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad )
	{
		Class popoverClass = NSClassFromString( @"UIPopoverController" );
		if( !popoverClass )
			return;

		_popoverViewController = [[popoverClass alloc] initWithContentViewController:picker];
		[_popoverViewController setDelegate:self];
		//picker.modalInPopover = YES;

		// Display the popover
		[_popoverViewController presentPopoverFromRect:CGRectMake( 0, 0, 128, 128 )
												inView:UnityGetGLViewController().view
							  permittedArrowDirections:UIPopoverArrowDirectionAny
											  animated:YES];
	}
	else
	{
		// wrap and show the modal
        UIViewController *vc = UnityGetGLViewController();
		[vc presentModalViewController:picker animated:YES];
	}
}
- (void)popoverControllerDidDismissPopover:(UIPopoverController*)popoverController
{
	self.popoverViewController = nil;
	//UnityPause( false );

	//UnitySendMessage( "EtceteraManager", "imagePickerDidCancel", "" );
}

- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
{
	// Grab the image and write it to disk
	UIImage *image;
	UIImage *image2;
    //	if( _pickerAllowsEditing )
    image = [info objectForKey:UIImagePickerControllerEditedImage];
    //        else
    //            image = [info objectForKey:UIImagePickerControllerOriginalImage];

    //NSLog( @"picker got image with orientation: %i", image.imageOrientation );
    UIGraphicsBeginImageContext(CGSizeMake(128,128));
    [image drawInRect:CGRectMake(0, 0, 128, 128)];
    image2 = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    // 得到了image,然后用你的函数传回u3d
    NSData *imgData;
    if(UIImagePNGRepresentation(image2) == nil)
    {
         imgData= UIImageJPEGRepresentation(image2, 0.5);
    }
    else
    {
         imgData= UIImageJPEGRepresentation(image2, 0.5);
    }

    NSString *_encodeImageStr = [imgData base64Encoding];

    UnitySendMessage( "UnityIOSBridge", "PhotoCallBack", _encodeImageStr.UTF8String);
	// Dimiss the pickerController
	[self dismissWrappedController];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController*)picker
{
	// dismiss the wrapper, unpause and notifiy Unity what happened
	[self dismissWrappedController];
	//UnityPause( false );
	//UnitySendMessage( "EtceteraManager", "imagePickerDidCancel", "" );
}

- (void)dismissWrappedController
{
	//UnityPause( false );

	UIViewController *vc = UnityGetGLViewController();

	// No view controller? Get out of here.
	if( !vc )
		return;

	// dismiss the view controller
	[vc dismissModalViewControllerAnimated:YES];

	// remove the wrapper view controller
	[self performSelector:@selector(removeAndReleaseViewControllerWrapper) withObject:nil afterDelay:1.0];

	//UnitySendMessage( "EtceteraManager", "dismissingViewController", "" );
}

- (void)removeAndReleaseViewControllerWrapper
{
	// iPad might have a popover
	if( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad && _popoverViewController )
	{
		[_popoverViewController dismissPopoverAnimated:YES];
		self.popoverViewController = nil;
	}
}
@end

extern "C"
{
    void openPhoto()//相册
    {
        //UnityPause( true );
        OpenPhotoController * app = [[OpenPhotoController alloc] init];
        // No need to give a choice for devices with no camera
        if( ![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] )
        {
            [app showPicker:UIImagePickerControllerSourceTypeSavedPhotosAlbum];
            return;
        }
        [app showActionSheet];

    }

}

unity中调用方法

直接把这俩文件放在Plugins的iOS目录下,或者打包出Xcode工程后再导入这俩文件也可以,推荐第一种,做版本控制的时候比较方便

[DllImport("__Internal")]

private static extern void openPhoto();

public void OpenPhoto(string vName)

{

#if UNITY_IPHONE

openPhoto();

#endif

}

注:

imgData= UIImageJPEGRepresentation(image2, 0.5);是压缩文件 0.5是压缩质量

上传本地图片部分搞定!

时间: 2024-11-05 11:43:01

Unity3d本地上传并且裁剪图片-----iOS平台的相关文章

Unity3d本地上传并且裁剪图片-----Android平台

注:引擎版本unity4.x 最近项目需求,需要做用户头像,要求: 1.  可以从本地上传 2.  本地裁剪 3.  压缩控制大小 4.  在三个平台实现PC/Android/IOS 弄了好几天总是搞完了总结一下 从本地上传会用到Android系统功能,打开相册和用摄像机拍照,因此unity和Android的交互是必须要会的. Unity-android可参考到宣雨松的博客http://www.xuanyusong.com/archives/676package com.cheerflame.s

MVC使用JCrop上传、裁剪图片

JCrop用来裁剪图片,本篇想体验的是: 在视图页上传图片: 上传成功,跳转到另外一个编辑视图页,使用JCrop对该图片裁剪,并保存图片到指定文件夹: 裁剪成功后,在主视图页显示裁剪图片: 当然,实际项目中最有可能的做法是:在本页上传.裁剪并保存. □ 思路 →在上传图片视图页,把图片上传保存到一个临时文件夹Upload→在编辑裁剪视图页,点击"裁剪"按钮,把JCrop能提供的参数,比如宽度.高度.离顶部距离,离底部距离,离左右端距离等封装成类,传递给控制器方法→控制器方法根据接收到的

图片本地上传预览

在IE里面显示的话,你可以用div直接显示图片,不通过img来添加SRC,这样就不会有小图标出现 例子: <div class="banner_up_pic" id="imgPreDiv"></div> var file_upl = document.getElementById("file"); file_upl.select(); 获取图片路径  var imgpath=document.selection.creat

SpringMVC上传文件(图片)并保存到本地

SpringMVC上传文件(图片)并保存到本地 小记一波~ 基本的MVC配置就不展示了,这里给出核心代码 在spring-mvc的配置文件中写入如下配置 <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize">

Dedecms本地上传缩略图无法自动添加水印的解决方法

客户遇到一个问题,DEDECMS(V5.7)后台添加文档时,本地上传缩略图无法自动添加水印(系统设置里的图片水印设置没有问题),找了半天,终于找到了解决方法,留个记号: 打开dede/archives_do.php 找到第111行 $upfile = AdminUpload('litpic', 'imagelit', 0, false ); 改成 $upfile = AdminUpload('litpic', 'imagelit', 0, true ); 然后打开include/helpers/

input type=&#39;file&#39; 上传文件 判断图片的大小是否合格与witdh 和 height 是否合格

function CheckFiles(obj) { var array = new Array('gif', 'jpeg', 'png', 'jpg'); //可以上传的文件类型 if (obj.value == '') { alert("让选择要上传的图片!"); return false; } else { var fileContentType = obj.value.match(/^(.*)(\.)(.{1,8})$/)[3]; //这个文件类型正则很有用:) var isE

上传头像裁剪功能

我之前做项目的时候有需求是需要实现上传头像裁剪再上传到服务器,所以上网看了一下别人写的案例.方法.文章,浪费了1天的时间,最后才跌跌碰碰的终于写了出来. 现在给大家分享一下吧. 所用到的js文件 cropper.min.js, canvas-to-blob.js(这个是IE上需要用到的,不然会不支持转blob) jquery.min.js, bootstrap.min.js ajaxfileupload.js css文件 cropper.min.css,bootstrap.min.css htm

从本地上传到hdfs上出现异常

hdfs dfs -put  从本地上传到hdfs上出现异常 与namenode  同台机器的datanode错误日志信息如下: 2015-12-03 09:54:03,083 WARN org.apache.hadoop.hdfs.server.datanode.DataNode: Slow BlockReceiver write data to disk cost:727ms (threshold=300ms) 2015-12-03 09:54:03,991 INFO org.apache.

本地上传文件到服务器,从服务器下载文件到本地

最近在做项目的时候涉及到了文件的上传.下载,以前学习IO时也没有搞得多清楚,在网上找了些上传下载的例子,然后修改了部分.经测试,上传下载文件暂时能用,下面是上传和下载的方法: 1.本地上传文件到服务器 html代码: <form id="uploadDatumInfo" name="uploadDatumInfo" method="post" enctype="multipart/form-data" target=&q