Memory issues using ASIHTTPRequest or Image Resizing

I can't tell where but I'm having memory issues uploading images to my server. After about 10 or so successful uploads i get memory warnings of level 1 then the next time it quits due DateFormatters or basically being out of memory. Here is my code. It uses ASIHTTPDataRequest and a image resize function. I cant tell if its the request thats not being released or if its the image resizing function not releasing the image. The images are huge photos taken from the phone at their normal high resolution then resized to print size and uploaded. Somewhere along the line i'm missing some memory management somewhere and its driving me mad!

Im getting memory warnings after about 10 or more uploads to my server using the following code. Is there some memory management specific to ASI that I'm not using?

Hi I'm using your software but im running into huge memory issues. I get memory warnings and i'm not sure how to clear them out without breaking the upload. I was wondering if you could take a look at this code sample and maybe show me what im doing wrong that causes the memory issue?

Im getting memory warnings after about 10 or more uploads to my server using the following code. Is there some memory management specific to ASI that I'm not using?

Hi I'm using your software but im running into huge memory issues. I get memory warnings and i'm not sure how to clear them out without breaking the upload. I was wondering if you could take a look at this code sample and maybe show me what im doing wrong that causes the memory issue?

Im getting memory warnings after about 10 or more uploads to my server using the following code. Is there some memory management specific to ASI that I'm not using?

Hi I'm using your software but im running into huge memory issues. I get memory warnings and i'm not sure how to clear them out without breaking the upload. I was wondering if you could take a look at this code sample and maybe show me what im doing wrong that causes the memory issue?

-(void)uploadPhoto:(UploadObject*)upObject{
 UIImage *image=[UIImage imageWithContentsOfFile:[upObject.imageObj.imageList objectAtIndex:0]];
 if([upObject.imageObj.imgType isEqualToString:@"SINGLE"]){
  image=[self image:image ByScalingAndCroppingForSize:CGSizeMake(1800, 1200)];
 }else if([upObject.imageObj.imgType isEqualToString:@"STANDARD8X10"]){
  image=[self image:image ByScalingAndCroppingForSize:CGSizeMake(3000, 2400)];
 }else if([upObject.imageObj.imgType isEqualToString:@"MOUSE"]){
  image=[self image:image ByScalingAndCroppingForSize:CGSizeMake(2850, 2400)];
 }else{
  image=[self image:image ByScalingAndCroppingForSize:CGSizeMake(1800, 1200)];
 }
 NSFileManager *fileManager = [NSFileManager defaultManager];
 [fileManager setDelegate:self];
 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
 NSString *documentsDirectory = [paths objectAtIndex:0];
 NSData *data = [NSData dataWithData:UIImageJPEGRepresentation(image, 100.0f)];//1.0f = 100% quality
 [data writeToFile:[upObject.imageObj.imageList objectAtIndex:0] atomically:YES];
 __block ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:@"http://api.postalpix.com/order/new"]];
 [request setPostValue:upObject.imageObj.pixID forKey:@"uid"];
 [request setPostValue:upObject.orderID forKey:@"orderid"];
 [request setPostValue:upObject.cost forKey:@"total"];
 [request setPostValue:@"NONE" forKey:@"coupon"];
 [request setPostValue:[NSString stringWithFormat:@"%d",upObject.total] forKey:@"images"];
 [request setPostValue:upObject.block forKey:@"block"];
 [request setPostValue:currentCoupon forKey:@"coupon"];
 [request setPostValue:[NSString stringWithFormat:@"%d",[upObject.imageObj.copies intValue]] forKey:@"quantity"];
 [request setTimeOutSeconds:20];
 request.showAccurateProgress = YES;
 [request setUploadProgressDelegate:self];
 [request setDelegate:self];
 [request setFile:[upObject.imageObj.imageList objectAtIndex:0] forKey:@"upfile"];
 if ([upObject.imageObj.imgType isEqualToString:@"SINGLE"])
 {
  [request setPostValue:@"29375" forKey:@"sku"];
  [request setPostValue:@"4x6 Print" forKey:@"description"];

 }else if ([upObject.imageObj.imgType isEqualToString:@"MOUSE"])
 {
  [request setPostValue:@"28925" forKey:@"sku"];
  [request setPostValue:@"Photo Mouse Pad" forKey:@"description"];
 }else if ([upObject.imageObj.imgType isEqualToString:@"STANDARD8X10"])
 {
  [request setPostValue:@"29376" forKey:@"sku"];
  [request setPostValue:@"8x10 Print" forKey:@"description"];
 }
 request.shouldContinueWhenAppEntersBackground=YES;
 [request setCompletionBlock:^{
  NSDictionary *jo=[[request responseString] JSONValue];
  NSFileManager *fileManager = [NSFileManager defaultManager];
  [fileManager removeItemAtPath:[currentUp.imageObj.imageList objectAtIndex:0] error:NULL];
  [self removeFinishedUpload:currentUp.block];
  currentUp=[self getNextUploadObject];
  if(currentUp){
   [self uploadPhoto:currentUp];
  }else{
   currentUploadCount=0;
   [[NSNotificationCenter defaultCenter] postNotificationName:@"UPDATEMOREBADGE" object:nil];
   isUploading=NO;
   [[NSNotificationCenter defaultCenter] postNotificationName:@"HIDE_PROGRESS" object:nil];
  }
 }];
 [request setFailedBlock:^{
  NSError *error = [request error];
  [self restartUpload];
 }];
 [request startAsynchronous];
}


- (UIImage*)image:(UIImage *)image ByScalingAndCroppingForSize:(CGSize)targetSize
{
 UIImage *sourceImage = image;
 UIImage *newImage = nil;        
 CGSize imageSize = sourceImage.size;
 CGFloat width = imageSize.width;
 CGFloat height = imageSize.height;
 CGFloat targetWidth = targetSize.width;
 CGFloat targetHeight = targetSize.height;
 CGFloat scaleFactor = 0.0;
 CGFloat scaledWidth = targetWidth;
 CGFloat scaledHeight = targetHeight;
 CGPoint thumbnailPoint = CGPointMake(0.0,0.0);

 if (CGSizeEqualToSize(imageSize, targetSize) == NO) 
 {
        CGFloat widthFactor = targetWidth / width;
        CGFloat heightFactor = targetHeight / height;

        if (widthFactor > heightFactor) 
   scaleFactor = widthFactor; // scale to fit height
        else
   scaleFactor = heightFactor; // scale to fit width
        scaledWidth  = width * scaleFactor;
        scaledHeight = height * scaleFactor;

        // center the image
        if (widthFactor > heightFactor)
  {
   thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5; 
  }
        else 
   if (widthFactor < heightFactor)
   {
    thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
   }
 }       

 UIGraphicsBeginImageContext(targetSize); // this will crop

 CGRect thumbnailRect = CGRectZero;
 thumbnailRect.origin = thumbnailPoint;
 thumbnailRect.size.width  = scaledWidth;
 thumbnailRect.size.height = scaledHeight;

 [sourceImage drawInRect:thumbnailRect];

 newImage = UIGraphicsGetImageFromCurrentImageContext();
 if(newImage == nil) 
      UIGraphicsEndImageContext();
 return newImage;
}

Answers


I think the line

if(newImage == nil) 
    UIGraphicsEndImageContext();

is not correct. The UIGraphicsEndImageContext() must be invoked with no condition.


Need Your Help

Codeigniter escapeshellarg() has been disabled for security reasons

php codeigniter

Is there any way to rewrite codeigniter's Upload.php library because I keep facing

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.