2012-12-26 78 views
0

我想在服务器上传我的大图像。
是否有可能将它们转换成块并上传到服务器?通过将图像分成小块来将大图像上传到服务器

这是我上传图片的代码我怎样才能发送图像块?

NSDateFormatter* df = [[NSDateFormatter alloc]init]; 
    [df setDateFormat:@"MM/dd/yyyy"]; 
    NSString *result = [df stringFromDate:assetDate]; 

    NSTimeInterval timeInterval = [assetDate timeIntervalSince1970]; 
    ALAssetRepresentation *rep = [temp defaultRepresentation]; 
    CGImageRef iref = [rep fullResolutionImage]; 
    StrPath = [StrPath stringByAppendingFormat:@"%d.%@",(int)timeInterval,strImageType]; 
    //UIImage *image = [UIImage imageWithCGImage:[rep fullResolutionImage]]; 

    UIImage *image =[UIImage imageWithCGImage:iref scale:[rep scale] orientation:(UIImageOrientation)[rep orientation]]; 
    //------------------ metadata ------------------------------------------------------- 
    NSDictionary *imageMetadata = [rep metadata]; 
    NSString *strOrt=[NSString stringWithFormat:@"%@",[imageMetadata valueForKey:@"Orientation"]]; 

    NSData *dataObj = nil; 
    dataObj = UIImageJPEGRepresentation(image, 1.0); 
    NSString* StrFileData = [Base64 encode:dataObj]; 
    NSString* strFileHash = [dataObj md5Test]; 

    NSMutableDictionary *DictRequest = [[NSMutableDictionary alloc]init]; 
    [DictRequest setObject:srtprefSessionId forKey:@"SessionId"]; 
    [DictRequest setObject:StrPath forKey:@"Path"]; 
    [DictRequest setValue:[NSNumber numberWithBool:isTrash] forKey:@"UploadDirectlyToTrashbin"]; 
    [DictRequest setObject:StrFileData forKey:@"FileData"]; 
    [DictRequest setObject:strFileHash forKey:@"FileHash"]; 
    [DictRequest setObject:result forKey:@"DateCreated"]; 

    BOOL isNULL = [self stringIsEmpty:strOrt]; 
    if(!isNULL) 
    { 
     //[DictRequest setObject:strOrt forKey:@"Orientation"]; 
    } 


    NSString *jsonString = [DictRequest JSONRepresentation]; 
    NSString *strUrl=[NSString stringWithFormat:@"%@",FileUpload_URL]; 
    NSURL *url1=[NSURL URLWithString:strUrl]; 
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url1]; 
    [request setTimeoutInterval:60.0]; 
    [request setHTTPMethod:@"POST"]; 
    NSData *postData = [jsonString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 
    NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]]; 
    [request setHTTPBody:postData]; 
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 


    if(theConnection) 
     [self Set2Defaults]; 

    theConnection = [[NSURLConnection alloc]initWithRequest:request delegate:self]; 

    [SVProgressHUD dismiss]; 

    if(theConnection) 
     webData = [NSMutableData data]; 
    else 
     NSLog(@"Connection Failed !!!"); 
+0

是的,你可以将图像分成小块和上传到服务器,你可以连接服务器上的图像 – Sumanth

+0

@Sumanth检查我的代码 – mshau

+0

你面临什么问题? – Sumanth

回答

0

当您向服务器发送数据时,数据包可能会丢失。丢包的原因可能是抖动,带宽过低,信道过度拥挤。 尽量小的图像尺寸大小,将其作为整体好让你可以在河道

0

原来的编码数据字节检索将您的图像UTF8字符串发送字符串中块和串联,并在服务器转换为图像结束。

0

使用转换将图像转换为数据,并异步将其发送到服务器上。

相关问题