Ios 在我的服务器上上载100多个图像时收到内存警告和应用程序崩溃

Ios 在我的服务器上上载100多个图像时收到内存警告和应用程序崩溃,ios,memory-leaks,didreceivememorywarning,memory-warning,Ios,Memory Leaks,Didreceivememorywarning,Memory Warning,我上传了100多个图像在服务器上使用以下代码,我收到了内存警告后上传了几张图像。请帮帮我。。 以下是我在服务器上上传图像的代码: -(void)uploadSingleFile { if(!self.objWebServices.bSyncWasOn) { NSLog(@"STOP upload Single File RETURN"); [self Call_SaveSyncedImageID]; return; }

我上传了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 !!!");
}
试试这个:

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

为什么需要这么多的手术

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);

}

设置
cachePolicy
以避免缓存

request.cachePolicy=NSURLRequestReloadIgnoringLocalCacheData


这是很多你想让别人免费调试的代码。你做了什么?你怀疑是什么问题?@Meena是的,你怀疑。但在137张图片之后,我仍然得到了相同的错误@9danuploaded@Meena解决此类问题的标准方法是静态分析(从Xcode“产品”菜单执行“分析”命令)。首先,您必须在返回之前释放
DictRequest
,上传完成后连接。还需要通过保留
webData
来确保它不会过早发布。@Meena这个怎么样?DictRequest=[[NSMutableDictionary alloc]init]autorelease]@Meena-then
DictRequest=[[NSMutableDictionary]
更合适。是的,我需要执行此操作,因为我需要为我的Web服务执行此操作。他们可能会占用更多内存。我尝试过使用此代码,但仍然收到相同的内存警告。您对此有何想法,或者是否有其他方法上载图像,以便我可以尝试其他选项