2012-12-21 39 views
2

上上傳超過100張照片我使用下面的代碼在服務器上上傳超過100個圖像和我收到內存警告上傳幾張圖片之後。請幫我.. 這裏是我的服務器上上傳圖片代碼:收到內存警告和應用程序崩潰,而我的服務器

-(void)uploadSingleFile 
{ 
    if(!self.objWebServices.bSyncWasOn) 
    { 
     NSLog(@"STOP upload Single File RETURN"); 
     [self Call_SaveSyncedImageID]; 
     return; 
    } 
    SyncItem* item = [arrSelSyncItemIDS objectAtIndex:nSyncItemIndex]; 

    if(item.nType == SYNC_STATUS_NOT_SELECTED || item.nType == SYNC_STATUS_SYNC_DONE) 
    { 
     nSyncItemIndex = nSyncItemIndex + 1; 
     if([arrSelSyncItemIDS count] > nSyncItemIndex) 
     { 
      NSLog(@"Called uploadSingleFile"); 
      [self uploadSingleFile]; 
      return; 
     } 
     else 
     { 
      //[self LoadImages:!bShowHideSyncImages]; 
      [SVProgressHUD dismiss]; 
      [BtnStopSync setTitle:@"Start sync" forState:UIControlStateNormal]; 
      self.objWebServices.bSyncWasOn = NO; 
      self.objWebServices.isFileUpload = NO; 

      if([[NSUserDefaults standardUserDefaults] boolForKey:@"Notification_State"]) // Setting Switch Bool 
      { 
       UIAlertView *Alert = [[UIAlertView alloc] initWithTitle:@"test" message:@"*** Sync completed successfully." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
       [Alert show]; 
      } 
     } 
    } 
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; 
    NSString *srtprefSessionId = [NSString stringWithFormat:@"%@",[prefs objectForKey:@"prefSessionId"]]; 

    BOOL isTrash = FALSE; 
    NSString *StrPath = @"/Pictures/";  //Change 
    ALAsset* temp = item.itemAsset; 
    NSDate* assetDate = (NSDate*)[temp valueForProperty:@"ALAssetPropertyDate"]; 

    NSString* strID = @""; 
    strID = [[temp valueForProperty:@"ALAssetPropertyURLs"] description]; 
    strID = [strID stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; 

    NSString* strImageType = @""; 
    if ([strID rangeOfString:@"jpg"].location != NSNotFound || [strID rangeOfString:@"JPG"].location != NSNotFound){ 
     strImageType = @"jpg"; 
    } 
    else if ([strID rangeOfString:@"png"].location != NSNotFound || [strID rangeOfString:@"PNG"].location != NSNotFound) 
     strImageType = @"png"; 


    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

這是一個很多你想讓別人免費調試的代碼。你做了什麼?你懷疑是什麼問題? –

回答

0

試試這個:

-(void)uploadSingleFile 
{ 
    @autoreleasepool 
    { 
    ... 
    ... 
    } 
} 
+0

我正在使用ARC,我還需要做@ autoreleasepool? @ 9dan – mshau

+0

@Meena是的。 http://stackoverflow.com/questions/9086913/objective-c-why-is-autorelease-autoreleasepool-still-needed-with-arc – 9dan

+0

好,謝謝@ 9dan讓我試試這個 – mshau

0

爲什麼需要這麼多的操作????

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

另外我建議你在發送前壓縮圖像,因爲NSData將是巨大的。這可以從服務器端進行管理

嘗試發送之前壓縮圖像:

CGFloat compression = 0.9f; 
NSData *imageData = UIImageJPEGRepresentation([item image], compression); 
while ([imageData length] > 700000 && compression > 0.1) { 
    // NSLog(@"Image size too big, compression more: current data size: %d bytes",[imageData length]); 
    compression -= 0.1; 
    imageData = UIImageJPEGRepresentation([item image], compression); 

} 
+0

是的,我需要做這個操作,因爲我需要它爲我的web服務 – mshau

+0

他們可能需要更多的內存 – DivineDesert

+0

我已經嘗試使用此代碼,但我仍然得到相同的內存警告 – mshau

0

搭建cachePolicy避免緩存

request.cachePolicy = NSURLRequestReloadIgnoringLocalCacheData

相關問題