ios - 将图像上传到服务器 IOS7 后应用程序崩溃

标签 ios iphone objective-c

您好,我正在将图像从我的设备上传到我的服务器,但在上传到服务器后它崩溃了。在代码中,我已经给出了这样的规定,比如在上传图像后它必须在添加此代码后返回主视图,它一直在崩溃。

上传代码..

    NSString *urlString = @"http://indianpoliticalleadersmap.com/IOS/upload/photos/upload.php";

    NSData *imageData = UIImageJPEGRepresentation(imageview.image,90);
    long imageSize = imageData.length;
    NSLog(@"SIZE OF IMAGE: %.2f Mb", (float)imageSize/1024/1024);
 if (imageSize > 4.194e+6) {

     UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Sorry" message:@"your image exceeds more then 4mb pls upload below 4mb image" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
     [alert show];
     [alert release];
    }
   else
  {
       NSString *urlString = @"url";

       NSMutableURLRequest *request = [[[NSMutableURLRequest alloc]init]autorelease];
       [request setURL:[NSURL URLWithString:urlString]] ;
       [request setHTTPMethod:@"POST"];

       NSString *boundary = @"---------------------------14737809831466499882746641449";
       NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
       [request addValue:contentType forHTTPHeaderField:@"Content-Type"];

       NSMutableData *body = [NSMutableData data];
       [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary]dataUsingEncoding:NSUTF8StringEncoding]];
       [body appendData:[@"Content-Disposition: form-data; name=\"userfile\"; 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:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary]dataUsingEncoding:NSUTF8StringEncoding]];
       [request setHTTPBody:body];

       NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
       NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

       NSLog(@"%@data",returnString);
       [self temp];
       [self performSelector:@selector(myMethod) withObject:nil afterDelay:3.0f];
       [self performSelector:@selector(dissMissViewController) withObject:self afterDelay:4.0f];
       alertTimer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(showAlert) userInfo:nil repeats:NO];

     }
   }

当我在警告消息中单击“确定”时弹出警告消息后应用程序崩溃应用程序崩溃请告诉我如何解决此问题。

  -(void)showAlert{

     UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Thanks" message:@"For uploading Image" delegate:self cancelButtonTitle:nil otherButtonTitles:@"ok", nil];
     [alert show];
     [alert release];
    }

谢谢。

最佳答案

我不知道你的崩溃日志,但如果你的应用程序在警报后崩溃。那么问题将是委托(delegate)。

您应该替换 delegate=nil 而不是 delegate=self

  UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Sorry" message:@"your image exceeds more then 4mb pls upload below 4mb image" delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
 [alert show];

关于ios - 将图像上传到服务器 IOS7 后应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22396130/

相关文章:

ios - 使用 iOS 设备作为特斯拉计(磁力计讨论)

objective-c - 显示文件选择器对话框

iphone - 从 MPMoviePlayerController 隐藏 StatusBar

iphone - 创建带有按钮的自定义调出 View

iphone - addSubview 和 removeFromSuperview 时的堆增长

ios - 防止自定义类被类别更改

iphone - 这个 UI 元素叫什么?苹果手机

ios - 当枚举大小写具有相同的关联值时,避免重复的代码行?

ios - 如何以编程方式激活 ios 通知设置?

ios - Cocos2D 现实重力?