如何解决收到的内存警告错误?

时间:2016-02-19 06:07:43

标签: ios objective-c

从手机摄像头捕获图像并上传到服务器时,应用程序崩溃并收到此错误:

[/BuildRoot/Library/Caches/com.apple.xbs/Sources/CoreUI/CoreUI-371.4/Bom/Storage/BOMStorage.c:522] <memory> is not a BOMStorage file
2016-02-19 10:31:53.413 Appname[520:141128] Received memory warning.
Message from debugger: Terminated due to memory issue.

代码:

UIImage *finalImage, *rotatedImage;
    finalImage = self.capturedimg;
    if (finalImage.imageOrientation == UIImageOrientationRight)
    {
        rotatedImage = [self imageRotatedByDegrees:finalImage deg:90];

    }else if (finalImage.imageOrientation == UIImageOrientationDown)
    {
        rotatedImage = [self imageRotatedByDegrees:finalImage deg:180];

    }else if (finalImage.imageOrientation == UIImageOrientationLeft)
    {
        rotatedImage = [self imageRotatedByDegrees:finalImage deg:-90];

    }else if (finalImage.imageOrientation == UIImageOrientationUp)
    {
        rotatedImage = [self imageRotatedByDegrees:finalImage deg:360];
    }
    imageView.image = rotatedImage;

-(void)sendImage:(id)sender{

CGFloat compression = 0.5f;
CGFloat maxCompression = 0.1f;
int maxFileSize = 250*1024;

imageData = UIImageJPEGRepresentation(imageView.image, compression);

while ([imageData length] > maxFileSize && compression > maxCompression)
{
    compression -= 0.1;
    imageData = UIImageJPEGRepresentation(imageView.image, compression);
}

NSString *dateString = [self.serviceConnector datenow];
NSString *urlString = [NSString stringWithFormat:@"%@",SinglechatImageUpload_Url];
[allObjectArray removeAllObjects];
NSDictionary  *tempDict = [NSDictionary dictionaryWithObjectsAndKeys:
                           // Object and key pairs
                           dateString, @"imageuploaddate",
                           self.CaptionTxtfield.text, @"imagecaption",
                           nil];
[allObjectArray addObject:tempDict];

[self chatimgUrlString:urlString postValue:allObjectArray imageData:imageData];
}

- (void)chatimgUrlString:(NSString*)urlStr postValue:(NSMutableArray*)array imageData:(NSData*)imgData{
NSString *jsonString;
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:array
                                                   options:NSJSONWritingPrettyPrinted
                                                     error:&error];
if (!jsonData) {
    NSLog(@"Got an error: %@", error);
} else {
    jsonString  = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",urlStr]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
                                                       cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
                                                   timeoutInterval:10];

[request setHTTPMethod:@"POST"];

NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];

NSMutableData *body = [NSMutableData data];

[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: form-data; name=\"userfile\"; filename=\".jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imgData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

//json sting parameter
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"jsonstring\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[jsonString dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

// close form
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

[request setHTTPBody:body];

NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (connection) {

    receivedData = nil;
}
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
receivedData = [[NSMutableData alloc]init];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
  [receivedData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
receivedData = nil;
connection = nil;
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
@try {
     //send the data to the delegate
 id jsonObjects = [NSJSONSerialization JSONObjectWithData:reciveddata options:NSJSONReadingMutableContainers error:nil];
 NSLog(@"json : %@",jsonObjects);
}
@catch (NSException *exception) {
    NSLog(@"Exception error didfinishload: %@",exception.reason);
    receivedData = [[NSMutableData alloc]init];
    [self.delegate requestReturnedData:receivedData];
}
}

1 个答案:

答案 0 :(得分:0)

尝试在while循环中添加Autoreleasepool块:

while ([imageData length] > maxFileSize && compression > maxCompression)
{
    @autoreleasepool {
        compression -= 0.1;
        imageData = UIImageJPEGRepresentation(imageView.image, compression);
    }
}