2010-12-03 67 views
2

我不知道在哪里,但我有内存问题上传图像到我的服务器。大约10次左右成功上传后,我得到了1级的内存警告,然后下一次退出时由于DateFormatters或基本上内存不足。这是我的代码。它使用ASIHTTPDataRequest和一个图像大小调整功能。我不能告诉如果它的请求没有被释放,或者如果它的图像大小调整功能不释放图像。图像是以正常的高分辨率从手机拍摄的巨大照片,然后调整为打印尺寸并上传。在某处,我错过了某处的内存管理,它让我疯狂!使用ASIHTTPRequest或图像调整大小的内存问题

我在使用以下代码将大约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

我觉得行

if(newImage == nil) 
    UIGraphicsEndImageContext(); 

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

+0

这是一个内存问题,如果我不调用它?因为它适用于其他领域。 – felixdkatt 2010-12-03 03:13:02