Button
和两个UIimage
吧。之后我们提取的相片会在UIimage上显示。 public void GetBigImage(string filename){
string path="file://"+platformPath+filename;
Debug.Log (path);
StartCoroutine (loadImage(path,true));
}
public void GetSmallImage(string filename){
string path="file://"+platformPath+filename;
Debug.Log (path);
StartCoroutine (loadImage(path,false));
}
public void OnClickButton(){
if(Application.platform != RuntimePlatform.OSXEditor){
_GetImage();
}
}
public string platformPath{
get{
string path=null;
if(Application.platform==RuntimePlatform.IPhonePlayer)//判断平台
{
path= Application.persistentDataPath.Substring (0, Application.persistentDataPath.Length - 5);//ios 平台 就会获取documents路径
path = path.Substring(0, path.LastIndexOf('/'))+"/Documents/";
}
else
{
path=Application.dataPath+"/GameData/";//pc平台 获取当前工程GameData/的路径 GameData需要自己新建
}
return path;
}
}
IEnumerator loadImage(string path,bool isBig){
WWW www = new WWW(path);
yield return www;
if (www.isDone && www.error == null) {
if(isBig){
Big_Spr = Sprite.Create (www.texture, new Rect (0, 0, www.texture.width, www.texture.height), new Vector2 (0, 0));
image.sprite = Big_Spr;
}else{
Small_Spr = Sprite.Create (www.texture, new Rect (0, 0, www.texture.width, www.texture.height), new Vector2 (0, 0));
SmallImage.sprite = Small_Spr;
}
} else {
if(!www.isDone)
{
Debug.Log("WWW IS NOT DONE");
}
if(www.error != null)
{
Debug.Log(www.error);
}
}
}
这段代码实际用的时候还是按实际情况再改下吧。类似GetBigImage
和GetSmallImage
两个函数,其实可以整合成一个函数。写成两个函数实在不好看。这里我只是为了研究下这个功能,所以具体的没再去优化下。
#import
#import
@interface GetImage : UIViewController<UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
+(GetImage*)getImageView;
-(void)CallPhoto;
-(void)RemaoveView;
-(NSString*)GetDate;
-(UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size;
-(void)saveImage:(UIImage *)currentImage withName:(NSString *)imageName;
@end
这里我定义了三个成员函数和一个类函数,比较重要的一点就是继承UIViewController
和后面那一串协议。有兴趣的可以自己去研究下。
//
// GetImage.m
// CamreaTest
//
// Created by Admin on 15/8/27.
// Copyright (c) 2015年 Admin. All rights reserved.
//
#import "GetImage.h"
#define New_Size CGSizeMake(128,128)
@implementation GetImage
static GetImage* _getImageView = NULL;
+(GetImage*)getImageView{
if(_getImageView == NULL){
_getImageView = [[GetImage alloc] init];
}
return _getImageView;
}
-(void)CallPhoto{
//添加视图控制器,和视图
UIViewController *_roottemp = UnityGetGLViewController();
[_roottemp addChildViewController:[GetImage getImageView]];
[_roottemp.view addSubview:[GetImage getImageView].view];
UIActionSheet *sheet;
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
sheet = [[UIActionSheet alloc] initWithTitle:@"选择图片" delegate:self cancelButtonTitle:@"取消"
destructiveButtonTitle:nil otherButtonTitles:@"拍照",@"从相册选择", nil];
}
else{
sheet = [[UIActionSheet alloc] initWithTitle:@"选择" delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"取消" otherButtonTitles:@"从相册选择", nil];
}
sheet.tag = 255;
[sheet showInView:self.view];
}
-(NSString*)GetDate{
NSDate * senddate=[NSDate date];
NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init];
[dateformatter setDateFormat:@"yyyyMMddHHmmss"];
NSString * locationString=[dateformatter stringFromDate:senddate];
return locationString;
}
- (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size{
// 创建一个bitmap的context
// 并把它设置成为当前正在使用的context
UIGraphicsBeginImageContext(size);
// 绘制改变大小的图片
[img drawInRect:CGRectMake(0, 0, size.width, size.height)];
// 从当前context中创建一个改变大小后的图片
UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
// 使当前的context出堆栈
UIGraphicsEndImageContext();
// 返回新的改变大小后的图片
return scaledImage;
}
-(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (actionSheet.tag == 255) {
NSUInteger sourceType = 0;
// 判断是否支持相机
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
if (buttonIndex == actionSheet.cancelButtonIndex)
{
return;
}
switch (buttonIndex) {
case 0:
// 相机
sourceType = UIImagePickerControllerSourceTypeCamera;
break;
case 1:
// 相册
sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
break;
}
}
else {
if (buttonIndex == 0) {
return;
} else {
sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
}
}
// 跳转到相机或相册页面
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.delegate = self;
imagePickerController.allowsEditing = YES;
imagePickerController.sourceType = sourceType;
imagePickerController.supportedInterfaceOrientations;
[self presentViewController:imagePickerController animated:YES completion:^{}];
}
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[picker dismissViewControllerAnimated:YES completion:^{}];
UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
/* 此处info 有六个值
* UIImagePickerControllerMediaType; // an NSString UTTypeImage)
* UIImagePickerControllerOriginalImage; // a UIImage 原始图片
* UIImagePickerControllerEditedImage; // a UIImage 裁剪后图片
* UIImagePickerControllerCropRect; // an NSValue (CGRect)
* UIImagePickerControllerMediaURL; // an NSURL
* UIImagePickerControllerReferenceURL // an NSURL that references an asset in the AssetsLibrary framework
* UIImagePickerControllerMediaMetadata // an NSDictionary containing metadata from a captured photo
*/
NSString* smallname = [@"Small" stringByAppendingString:[[self GetDate] stringByAppendingString:@".png"]];
[self saveImage:[self scaleToSize:image size:New_Size] withName:smallname];
UnitySendMessage("HeadSelect", "GetSmallImage", [smallname UTF8String]);
[self RemoveView];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self dismissViewControllerAnimated:YES completion:^{}];
[self RemoveView];
}
- (void) saveImage:(UIImage *)currentImage withName:(NSString *)imageName
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);//获取document路径
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:imageName];//拼接字符串 图片名称为pic
NSData *imageData = UIImagePNGRepresentation(currentImage);//UIImage到NSData格式转换
[imageData writeToFile:savedImagePath atomically:NO];//生成图片
}
//结束后从列表删除自己
-(void) RemoveView
{
[self removeFromParentViewController];
[self.view removeFromSuperview];
}
@end
先说下getImageView
吧,我为了在不同的类中获取到同一个窗口,把他弄成了单例。
至于GetData
这个纯粹是我想给图片命名加上当前日期
日期我原来是用XX-XX-XX XX:XX:XX这种格式,图片也能出来,但是用WWW加载的时候就出问题了。所以还是尽量不要用特殊符号吧!
scaleToSize
和SaveImage
这两个顾名思义就是缩小图片到指定大小以及保存图片到本机。
(PS:原来有考虑过UnitySendMessage直接传NSData,不过没搞定O。O,哪位大牛如果搞定了,希望分享一下。)
actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
这两个函数其实代码完全一样,主要是IOS版本问题,为了兼容下低版本,在这里我把两个都写了,不知道有没什么更好的办法。(测试过,用新版的就可以了,我把原来的删除了)
imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
void _GetImage(){
[[GetImage getImageView] CallPhoto];
}
这样就搞完了,如果有什么做的不好的地方,希望大家帮我指出来。
(小改了下,感觉getImageView,改完不需要弄成单例了,有空的时候我会去试下)