English 中文(简体)
记忆管理与从UIImagePicker公司节省的图像
原标题:Memory management with image saved from UIImagePickerController

我写的是用户自带照片的照片,然后通过一系列意见,利用导航控制器调整图像。 如果用户与前线照相照相照相照(在支持该照相的装置上作为违约),但当我重复这一过程时,我会半途经,在发出记忆警告后坠毁。

在对文书进行描述后,我看到,在使用低分辨率前照相机图像时,我的记忆足迹保持在约20-25兆赫,但在使用背照相机时,每次视的变化会增加33兆赫,直至其坠毁约350兆赫(4S)

Below is the code I m using to handle saving the photo to the documents directory, and then reading that file location to set the image to a UIImageView. The "read" portion of this code is recycled through several view controllers (viewDidLoad) to set the image I saved as the background image in each view as I go.

我已删除了我的所有图像修改法,以缩小这一差距,以尽量减少孤立问题的努力,我似乎无法找到这个问题。 如今,所有的照片都是在第一眼中拍摄的照片,然后将照片用作另外大约10个观点的背景形象,通过观点把这种照片作为用户门槛。

现在,高分辨率的照片显然将使用更多的记忆,但我不理解的是,为什么低分辨率的光照像似乎在像我那样使用更多的记忆,而高分辨率的光照相在坠毁之前越来越多地使用。

我如何拯救和阅读形象:

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];    
    jpgData = UIImageJPEGRepresentation(image, 1);

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
    NSString *documentsPath = [paths objectAtIndex:0];
    filePath = [documentsPath stringByAppendingPathComponent:@"image.jpeg"];
    [jpgData writeToFile:filePath atomically:YES];    

    [self dismissModalViewControllerAnimated:YES];
    [disableNextButton setEnabled:YES];

    jpgData = [NSData dataWithContentsOfFile:filePath];
    UIImage *image2 = [UIImage imageWithData:jpgData];
    [imageView setImage:image2];
}

现在我知道,我可以尝试扩大形象,而我只想这样做,我计划再研究一下,但我看不出为什么这样做没有做。 Maybe 我的印象是,阿革委在离开顶端时自动处理已分配的观点和看法。

谁能说明为什么我储存着我的设备记忆? (精干一米,完全超视) 难道我会想把ARC扔出窗户吗?

<>光线> 我如何在我的其他观点中要求形象

- (void)loadBackground
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
    NSString *documentsPath = [paths objectAtIndex:0];
    NSString *filePath = [documentsPath stringByAppendingPathComponent:@"image.jpeg"];
    UIImage *image = [UIImage imageWithContentsOfFile:filePath];
    [backgroundImageView setImage:image];

}

• 如何在我的看法控制人员之间建立联系:

“entergraph

<><><><>>>>

我的基本声明如下:

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
@interface PhotoPickerViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
{
    IBOutlet UIImageView *imageView;
    NSData *jpgData;
    NSString *filePath;
    UIImagePickerController *imagePicker;
    IBOutlet UIBarButtonItem *disableNextButton;
}


@end

If relevant, how I call up my image picker:

- (void)callCameraPicker
{


    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] == YES)
    {
        NSLog(@"Camera is available and ready");

        imagePicker.sourceType =  UIImagePickerControllerSourceTypeCamera;
        imagePicker.delegate = self;
        imagePicker.allowsEditing = NO;
        imagePicker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;

        NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]; for (AVCaptureDevice *device in devices) 
        {
            if([[UIScreen mainScreen] respondsToSelector:@selector(scale)] && [[UIScreen mainScreen] scale] == 2.0) 
            {
                imagePicker.cameraDevice = UIImagePickerControllerCameraDeviceFront;


            }
        }


        imagePicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;

        [self presentModalViewController:imagePicker animated:YES];

    }


    else
    {
        NSLog(@"Camera is not available");
        UIAlertView *cameraAlert = [[UIAlertView alloc] initWithTitle:@"Error" 
                                                              message:@"Your device doesn t seem to have a camera!" 
                                                             delegate:self cancelButtonTitle:@"Dismiss" 
                                                    otherButtonTitles:nil];
        [cameraAlert show];

    }
}

<><><><>>>>> 挂着眼光,实际上,它现在没有叫上这样一米,在<条码>上载荷Background,在<条码>上登见WillAppear,在<条码>上贴出我的条码>背底字。 我期望这样做会有所助益,但不会有所改观。

- (void)viewWillAppear:(BOOL)animated
{
    [self loadBackground];
}


- (void)viewDidDisappear:(BOOL)animated
{
    NSLog(@"ViewDidDisappear");
    backgroundImageView = nil;
}
最佳回答

UIImageView之间的关系不一定对每个人都有启发性。

<代码>UIImage是你图像数据的高度代表性,仅此就显示数据而言就没有任何意义。

<编码>UIImageView与合作,使您能展示形象。

没有理由出现<代码>的多重情况。 UIImageView不能显示。 这是一种 n和高效的做法,因为只有一层图像数据,由多种观点共享。

你似乎正在创建新的<条码>。 页: 1 因此,这在两个方面是通用设计差的:直截了当“UIImage”,再一次又一次从磁盘中重载同样的图像数据。

你的记忆问题确实是一个单独的问题,你没有适当公布你将图像数据输入、物体和>UIImageViews

从理论上讲,你应当能够采用第一个<代码>。 UIImage, 您从UIImagePickerController再接过,简单地将这一提法放在你们的意见上,而不必从磁盘上重载。

如果由于职能要求较高(例如,由于用户正在改变形象,而且你希望保持这种形象),需要从磁盘中提取和重载,那么,你需要确保你从自己的观点中删除先前的“IDView。 在交易方法中设置一个空白点,以便确认正在拆除和交易,并确保你制定任何<代码>。 Var reference to subviews (it listed belowcode>backgroundImageView is an iVar) are set to nil. 如果你没有适当删除<条码>背井离心的,则继续提及<条码>。 UIImage, 您将其图像财产制成。

问题回答

有两件事:,。 关于你颁布的法典:

  1. None of your view-callback implementations call super. That’s bad! Make extra sure that you are calling super in viewDidUnload and (if you implemented it) didReceiveMemoryWarning.
  2. Make sure you implement didReceiveMemoryWarning in a meaningful way!
  3. You really should not be re-creating that image over and over again! I assume you are not editing the actual image because you use JPEG compression on it which — even at 100% quality — will deteriorate your image with every save…
  4. Check your implementation of viewDidUnload make sure to set every of your IBOutlets to nil.

ARC不是Pixie DustTM! 这只是为了拯救你打字,它确实是而不是,使你无法设计和维护你的标语!

从您的提问中,我至少看一下你的形象:

image 1 <- image-view 1 <- view-controller 1 <- navigation-controller <- key window <- application

image 1 <- image-view 1 <- view 1 <- view-controller 1 <- navigation-controller <- key window <- application

每个观察控制员都重复这样做,对观察控制者、观点、形象和形象进行指数变化。 虽然你们必须持有不同的看法,对你的观察控制者进行形象调查,但我不能认为你为什么要重复同样的形象。

因此,你记忆消费的第一个轴心显然是不再产生同样图像数据的所有复印件——我估计,这将给你带来低hang记忆节余的一半。

接下来的一点是,如果不再提及,阿革委只能免除其物体所消耗的记忆。

记忆聪明,观点并非完全轻重的物体,而当你在 deep子上架设深厚的导航屏障时。

因此,你必须仔细提及这些观点,而不必如此。

The level, at which this has to take this is the view-controller. 最新time,should > 发生时,见到控制器实施viewDid Unload

Why the view-controller?

From what you described, the image itself is only referenced by the UIImageView — this is a bad choice, IMHO, but I digress…
UIViewController is designed to “know”, when its view is needed and when it’s safe to dispose of it — that’s why it implements didReceiveMemoryWarning and viewDidUnload:

如果记忆压力高,而且观点控制者的观点不是“筛选”的话,那么<条码>didReceiveMemoryWorning的根基实施就会让人们看到并打电话<条码>viewDid Unload。 之后。

因此,请打电话到<条码><<> ><> > > ><> > > > > 在你执行这两种方法时,。

此外,如果你有强烈的IBOutlets,提及观察者的观点,你的话,请must。 页: 1

在其核心<条码>上,国家机器是一家大型机器。 所有这些“亲人/双人”的退款都用于这些国家之间的过渡,大多数缺省执行情况都相当重要的簿记,以保持这一状态。

If you are not invoking them in your overrides, you˚ll end up in inconsistent states and bad things — like this out of memory crasher — happen.

只是单独制造双倍,并拯救你的图像。 在你成功运作后,数据(或)倍数。 利用地毯。





相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签