你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iphone開發:上傳圖片

iphone開發:上傳圖片

編輯:IOS開發綜合

上傳後,需要服務器那邊進行解碼保存為圖片。以後只需要用url來獲得這個圖片。

- (void)right_nav_baritem_return

{

//向服務器提交表單或者是數據

    int size = 204800;

    int current_size = 0;

    int actual_size = 0;

    NSData *data = UIImageJPEGRepresentation(image_photo,1.0);

    current_size = [data length];

    if (current_size > size)

    {

        actual_size = size/current_size;

        data = UIImageJPEGRepresentation(image_photo,actual_size);

    }

   

    activity_indicator = [[UIActivityIndicatorViewalloc] initWithFrame:CGRectMake(150, 130, 20, 20)];

    [activity_indicatorsetActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhite];

    [self.viewaddSubview:activity_indicator];

    [activity_indicatorstartAnimating];

    [selfperformSelectorInBackground:@selector(upload_user_picker_image:) withObject:data];

}

 


- (void)upload_user_picker_image:(NSData*)data

{

    NSString *lat_long = [NSStringstringWithFormat:@"{\"latitude\":\"%f\",\"longitude\":\"%f\"}",lattitude,longitude];

    NSString *urlString = @"http://pluginwebtest.shopal.jp/api/v1/upload1/add_upload_shop.php";

   

    NSMutableURLRequest *request= [[[NSMutableURLRequestalloc] init] autorelease];

    [request setURL:[NSURLURLWithString:urlString]];

    [request setHTTPMethod:@"POST"];

   

    NSString *boundary = @"0xKhTmLbOuNdArY";

    NSString *contentType = [NSStringstringWithFormat:@"multipart/form-data; boundary=%@",boundary];

    [request addValue:contentType forHTTPHeaderField: @"Content-Type"];

   

    NSMutableData *postbody = [NSMutableDatadata];

    NSMutableString *str=[[NSMutableStringalloc] init];

//第一個字段

    [str appendFormat:@"\r\n\r\n--%@\r\n" ,boundary];

    [str appendFormat: @"Content-Disposition:form-data; name=\"product_desc\"\r\n\r\n"];

    if (product_desc)

    {

        [str appendFormat:product_desc];

    }

    else

    {

         [str appendFormat:@"null"];

    }

//第二個字段

    [str appendFormat:@"\r\n\r\n--%@\r\n" ,boundary];

    [str appendFormat: @"Content-Disposition:form-data; name=\"product_category\"\r\n\r\n"];

    if (product_category)

    {

        [str appendFormat:product_category];

    }

    else

    {

        [str appendFormat:@"null"];

    }

//第三個字段

    [str appendFormat:@"\r\n\r\n--%@\r\n" ,boundary];

    [str appendFormat: @"Content-Disposition:form-data; name=\"lat_long\"\r\n\r\n"];

    [str appendFormat:lat_long];

//第四個字段

    NSString *image_data = [data base64Encoded];

    [str appendFormat:@"\r\n\r\n--%@\r\n" ,boundary];

    [str appendFormat: @"Content-Disposition:form-data; name=\"image\"\r\n\r\n"];

    [str appendFormat:image_data];

//編碼

    [postbody appendData:[str dataUsingEncoding:NSUTF8StringEncoding]];

    [request setHTTPBody:postbody];

    [str release];

   

    NSData *returnData = [NSURLConnectionsendSynchronousRequest:request returningResponse:nilerror:nil];

    NSString *returnString = [[NSStringalloc] initWithData:returnData encoding:NSUTF8StringEncoding];

    NSLog(@"returnString is %@",returnString);

    [returnString release];

   

    [activity_indicatorstopAnimating];

    UIAlertView *alert = [[UIAlertViewalloc] initWithTitle:@"レポート" message:@"アップロードが成功寫真\n上傳成功。" delegate:self cancelButtonTitle:@"決定"otherButtonTitles:nil, nil];

    [alert setDelegate:self];

    [alert show];

    [alert release];

}

 

 

摘自 雲懷空-abel

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved