參考:http://www.tuicool.com/articles/vQR7B3ios
一、在ios/AppController.h 添加這兩個協議 UIImagePickerControllerDelegate,UINavigationControllerDelegateui
二、編寫下面代碼
atom
//顯示照相機 +(void) showImagePicker:(NSDictionary *)info { callBackId = [[info objectForKey:@"listener"] intValue]; UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init]; imagePicker.delegate = self; imagePicker.allowsEditing = true; imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:imagePicker animated:YES completion:nil]; } //選取照片完成 +(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { NSLog(@"%@",info); if ([[info objectForKey:UIImagePickerControllerMediaType] isEqualToString:@"public.image"]) { UIImage *img = [info objectForKey:UIImagePickerControllerEditedImage]; [self saveImage:img]; } else { } [picker dismissViewControllerAnimated:YES completion:nil]; } // 取消選擇 +(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { [picker dismissViewControllerAnimated:YES completion:nil]; } //保存圖片 +(void)saveImage:(UIImage *)img { BOOL success; NSFileManager *fileManager = [NSFileManager defaultManager]; NSError *error; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *imgFilePath = [documentsDirectory stringByAppendingPathComponent:@"headPhoto.png"]; success = [fileManager fileExistsAtPath:imgFilePath]; std::string newImgPath = cocos2d::CCFileUtils::getInstance()->getWritablePath() + "/headPhoto.png"; if (success) { success = [fileManager removeItemAtPath:[NSString stringWithFormat:@"%s",newImgPath.c_str()] error:&error]; NSLog(@"success 2 %d",success); } // 更改尺寸 UIImage * smallImage = [self thumbnailWithImageWithoutScale:img size:CGSizeMake(80, 80)]; [UIImageJPEGRepresentation(smallImage, 1.0f) writeToFile:[NSString stringWithFormat:@"%s",newImgPath.c_str()] atomically:YES]; // 新的照片地址回調給lua cocos2d::LuaObjcBridge::pushLuaFunctionById(callBackId); // cocos2d::LuaObjcBridge::getStack()->pushString([imgFilePath cStringUsingEncoding:NSUTF8StringEncoding]); cocos2d::LuaObjcBridge::getStack()->pushString(newImgPath.c_str()); cocos2d::LuaObjcBridge::getStack()->executeFunction(1); cocos2d::LuaObjcBridge::releaseLuaFunctionById(callBackId); } // 實現縮略圖 +(UIImage *) thumbnailWithImageWithoutScale:(UIImage *)img size:(CGSize)asize { UIImage * newImg; if (nil == img) { newImg = nil; }else{ CGSize oldsize = img.size; CGRect rect; if(asize.width/asize.height > oldsize.width/oldsize.height){ rect.size.width = asize.height * oldsize.width / oldsize.height; rect.size.height = asize.width; rect.origin.x = (asize.width - rect.size.width) / 2; rect.origin.y = 0; }else{ rect.size.width = asize.width; rect.size.height = asize.width * oldsize.height / oldsize.width; rect.origin.x = 0; rect.origin.y = (asize.height - rect.size.height) / 2; } UIGraphicsBeginImageContext(asize); CGContextRef content = UIGraphicsGetCurrentContext(); CGContextSetFillColorWithColor(content, [[UIColor clearColor] CGColor]); UIRectFill(CGRectMake(0, 0, asize.width, asize.height)); [img drawInRect:rect]; newImg = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); } return newImg; }
三、在lua裏面實現lua
if device.platform == "ios" then local function callBack( imgFileName ) //從新加載文理 cc.Director:getInstance():getTextureCache():reloadTexture(imgFileName) display.newSprite(imgFileName):addTo(self):pos(display.cx,display.cy) end luaoc.callStaticMethod("AppController","showImagePicker",{listener = callBack}) end