使用ASIHTTPRequest或图像大小调整的内存问题

时间:2010-12-03 00:34:23

标签: iphone file-upload asihttprequest

我无法分辨到哪里,但我在将图像上传到服务器时遇到内存问题。大约10次左右成功上传后,我得到级别1的内存警告,然后下次退出DateFormatters或基本上内存不足。这是我的代码。它使用ASIHTTPDataRequest和图像大小调整功能。我无法判断它的请求是否未被释放或者图像大小调整功能是否未释放图像。这些图像是以正常的高分辨率从手机拍摄的巨幅照片,然后调整大小以打印尺寸并上传。在某个地方,我错过了一些内存管理,这让我很生气!

使用以下代码将大约10次或更多次上传到我的服务器后,我收到了内存警告。是否有一些我不使用的特定于ASI的内存管理?

您好我正在使用您的软件,但我遇到了巨大的内存问题。我得到了内存警告,我不知道如何在不中断上传的情况下清除它们。 我想知道你是否可以看看这个代码示例,也许可以告诉我我做错了什么导致了内存问题?

使用以下代码将大约10次或更多次上传到我的服务器后,我收到了内存警告。是否有一些我不使用的特定于ASI的内存管理?

您好我正在使用您的软件,但我遇到了巨大的内存问题。我得到了内存警告,我不知道如何在不中断上传的情况下清除它们。 我想知道你是否可以看看这个代码示例,也许可以告诉我我做错了什么导致了内存问题?

使用以下代码将大约10次或更多次上传到我的服务器后,我收到了内存警告。是否有一些我不使用的特定于ASI的内存管理?

您好我正在使用您的软件,但我遇到了巨大的内存问题。我得到了内存警告,我不知道如何在不中断上传的情况下清除它们。 我想知道你是否可以看看这个代码示例,也许可以告诉我我做错了什么导致了内存问题?

-(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;
}

1 个答案:

答案 0 :(得分:1)

我认为该行

if(newImage == nil) 
    UIGraphicsEndImageContext();

不正确。必须在没有条件的情况下调用UIGraphicsEndImageContext()