我有一个使用ELCImagePicker的多图像选择器的钛应用程序。我设置了一切,它运行良好。
我想将其他参数传递给模块,但由于参数不是全局参数,因此在使用过程中会出现错误。
Titanium Code
var my_module = require('jp.kray.ti.ELCImagePicker');
my_module.loadImagePicker({
iid:id,
domain:'http://myhost/upload.php',
success:function(data){
...
ObjectiveC代码
- (void)loadImagePicker:(id)args {
ENSURE_UI_THREAD(loadImagePicker,args);
ENSURE_SINGLE_ARG_OR_NIL(args,NSDictionary);
NSLog(@"Load ImagePicker\n");
if (args != nil) {
// callbacks
if ([args objectForKey:@"success"] != nil) {
pickerSuccessCallback = [args objectForKey:@"success"];
ENSURE_TYPE_OR_NIL(pickerSuccessCallback,KrollCallback);
[pickerSuccessCallback retain];
}
if ([args objectForKey:@"cancel"] != nil) {
pickerCancelCallback = [args objectForKey:@"cancel"];
ENSURE_TYPE_OR_NIL(pickerCancelCallback,KrollCallback);
[pickerCancelCallback retain];
}
NSString *uploadUrl = [TiUtils stringValue:[args objectForKey:@"domain"]];
NSString *iid = [TiUtils stringValue:[args objectForKey:@"iid"]];
}
_albumController = [[ELCAlbumPickerController alloc] init];
ELCImagePickerController *elcPicker = [[ELCImagePickerController alloc] initWithRootViewController:_albumController];
[_albumController setParent:elcPicker];
_albumController.assetFilter = kELCAlbumAllAssets;
_albumController.cellHeight = 75;
_albumController.titleForSelection = NSLocalizedString(@"Pick Something", @"Title for picking items");
[elcPicker setDelegate:self];
TiApp *tiApp = [TiApp app];
[tiApp showModalController:elcPicker animated:YES];
[elcPicker release];
}
控制器中的问题代码
- (void)elcImagePickerController:(ELCImagePickerController *)picker didFinishPickingMediaWithInfo:(NSArray *)info {
NSLog(@"success didFinish:\n");
if (pickerSuccessCallback != nil) {
id listener = [[pickerSuccessCallback retain] autorelease];
NSMutableArray *images = [NSMutableArray array];
for (NSDictionary *dict in info) {
UIImage *image = [dict objectForKey:UIImagePickerControllerOriginalImage];
//Activate the status bar spinner
UIApplication* app = [UIApplication sharedApplication];
app.networkActivityIndicatorVisible = YES;
//The image you want to upload represented in JPEG
//NOTE: the 'selectedPhoto' needs to be replaced with the UIImage you'd like to upload
NSData *imageData = UIImageJPEGRepresentation(image, 1);
//NOTE: Change this to the upload URL you're posting to
//NSString *uploadUrl = @"http://"+[NSString *domain]+"/upload.php";
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
//[request setURL:[NSURL URLWithString:uploadUrl]];
[request setURL:[NSURL URLWithString:@"http://localhost/upload.php"]];
[request setHTTPMethod:@"POST"];
NSMutableData *body = [NSMutableData data];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request addValue:contentType forHTTPHeaderField:@"Content-Type"];
//The file to upload
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"image\"; filename=\".jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// another text parameter
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"parameter2\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:iid] <---- Here ---->
找不到iid。我试图声明一个全局变量并使用loadImagePicker方法覆盖它,但它仍然在抱怨。
谢谢
答案 0 :(得分:0)
好的感谢您的帮助,但我得到了它的工作。在Objective C中看起来情况略有不同,所以这是我的解决方案。
#import "TiModule.h"
#import "KrollCallback.h"
#import "ELCAlbumPickerController.h"
@interface JpKrayTiELCImagePickerModule : TiModule {
KrollCallback *pickerSuccessCallback;
KrollCallback *pickerErrorCallback;
KrollCallback *pickerCancelCallback;
ELCAlbumPickerController *_albumController;
NSString *iid;
}
@property(nonatomic,retain)NSString *iid;
+(JpKrayTiELCImagePickerModule*)getInstance;
@end
和我的实施文件
@implementation JpKrayTiELCImagePickerModule
//(nonatomic)
@synthesize iid;
@synthesize domain;
static JpKrayTiELCImagePickerModule *instance = nil;
+(JpKrayTiELCImagePickerModule *)getInstance
{
@synchronized(self)
{
if(instance==nil)
{
instance= [JpKrayTiELCImagePickerModule new];
}
}
return instance;
}
我称之为加载方法
if ([args objectForKey:@"iid"] != nil) {
JpKrayTiELCImagePickerModule *obj=[JpKrayTiELCImagePickerModule getInstance];
obj.iid= [TiUtils stringValue:@"iid" properties:args def:@"1234"];
}
代码中的任何地方,当我想使用Titaium App中的这些变量时。
JpKrayTiELCImagePickerModule *obj=[JpKrayTiELCImagePickerModule getInstance];
[NSString stringWithString:obj.iid]
希望这有助于某人。