Xcode 应用没有释放内存

Xcode App doesn't release memory

我正在创建一个将图像存储在设备中的应用程序(将其保存为 coreData),但我遇到了问题。 每次选择一张图片加入collectionView,内存增加100Mb左右,而且每次都增加。 我想我添加了 ARC , (Edit->refactor->convert to objective-c ARC..) 添加它似乎没有任何问题,但内存仍然没有释放。 当用户在 uiimagepicker 中选择图像时,这是我的代码:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

    UIImage *chosenImage = info[UIImagePickerControllerOriginalImage];

    NSData *imageData = UIImageJPEGRepresentation(chosenImage ,0.2);

    if (imageData != nil)
    {
        AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;

        NSString *urlString = [NSString stringWithFormat:@"myurl.."];
        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
        [request setURL:[NSURL URLWithString:urlString]];
        [request setHTTPMethod:@"POST"];

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

        NSMutableData *body = [NSMutableData data];
        [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundry] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithFormat:@"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:imageData]];
        [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundry] dataUsingEncoding:NSUTF8StringEncoding]];
        [request setHTTPBody:body];

        //NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
        //NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

        NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
        NSLog(@"finish uploading");


        NSManagedObjectContext *context = [self managedObjectContext];
        NSManagedObject *newImage = [NSEntityDescription insertNewObjectForEntityForName:@"Images" inManagedObjectContext:context];
        [newImage setValue:imageData forKey:@"image"];

        NSError *error = nil;
        // Save the object to persistent store
        if (![context save:&error]) {
            NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Opps" message:@"There was an error saving the photo.. try again!" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
            [alert show];
        }


        [photos addObject:imageData];

        [self.collectionView reloadData];


    }

    [picker dismissViewControllerAnimated:YES completion:NULL];

}
  1. 当这些图像在集合视图单元格中使用时,您应该使用与单元格中图像视图大小(乘以设备比例)相对应的图像尺寸。因此,如果图像视图为 100x100 pt,比例为 2.0,您可能希望在将图像加载到集合视图单元格时将其调整为 200x200。

    如果不这样做,当您拍摄高分辨率图像并在单元格中使用它时,占用的内存量是基础图像尺寸的倍数,而不是图像视图中图像视图的大小细胞。通常每个像素需要四个字节,例如一张 3000x2000 像素的图像将占用 24mb,而如果将其调整为 200x200 图像,则将占用 160kb。但是请记住,使用图像时的内存量远远大于 UIImageJPEGRepresentation.

    的大小

    This is the image resizing code I use, but feel free to resize it any way you want. Here is a .

  2. 虽然图像的大小可能是主要问题,但应注意您不应将图像数据保存在数组中。您应该保存一些 identifier/path 可用于从缓存 and/or 持久存储中检索图像。但是不要试图一次将与图像相关联的实际数据保存在内存中。

    顺便说一句,如果您出于性能原因确实使用了 NSCache 之类的东西,请确保在内存不足时清空该缓存。

  3. 顺便说一句,如果您调整图像大小,则可以减少在 JPEG 压缩中使用如此低质量的需求(这会导致图像质量下降)。与牺牲 JPEG 压缩质量相比,图像尺寸对内存使用的影响通常要大得多。