在 iPhone 中使用 UIImagePickerControllerSourceTypeCamera 捕获图像 10 或 11 次后收到内存警告

发布于 2024-11-08 01:40:51 字数 3205 浏览 0 评论 0原文

您好,每当我使用相机时,我都会收到内存警告。

错误是这样的:

"Receive memory warning..."

代码是:

-(void) getPhoto{

    GameAppdelegate *appDelegate = (GameAppdelegate *)[[UIApplication sharedApplication]delegate];

    UIImagePickerController * picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;

    ///////////////////////////////////photolibrary//////////////////////////////
    if([appDelegate.photoselection isEqualToString:@"User Pressed Button 1\n"])
    {
        picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;

        if(appDelegate.sound == 1)
        {
            [classObj ButtonSound];
        }
    }
    ///////////////////////////////////Camera//////////////////////////////

    else if([appDelegate.photoselection isEqualToString:@"User Pressed Button 2\n"]) 
    {

        @try 
        {
            picker.sourceType = UIImagePickerControllerSourceTypeCamera;
        }
        @catch (NSException * e)
        {
            UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"ALERT" 
                                                         message:@"Please try again"
                                                        delegate:self 
                                               cancelButtonTitle:nil 
                                               otherButtonTitles:@"ok", nil];
            [av show];

        }
        if(appDelegate.sound == 1)
        {
            [classObj ButtonSound];
        }
    }
    ///////////////////////////////////Cancel//////////////////////////////


    else if([appDelegate.photoselection isEqualToString:@"User Pressed Button 3\n"]) 
    {
        if(appDelegate.sound == 1)
            [classObj ButtonSound];
        return;
    }
    [self presentModalViewController:picker animated:YES];
    [picker release];
}

我该如何处理这个?拍照后请帮助我裁剪图像并保存在应用程序中,如下所示:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {

    iRolegameAppDelegate *appDelegate = (iRolegameAppDelegate *)[[UIApplication sharedApplication]delegate];

    if(appDelegate.sound == 1)
    {
        [classObj ButtonSound];
    }

    [picker dismissModalViewControllerAnimated:YES];
    imageView.image = image;
    CGSize size = [imageView.image size];
    CGRect cropRect = CGRectMake(0.0, 0.0, size.width, size.height);

    NSValue *cropRectValue = [editingInfo objectForKey:@"UIImagePickerControllerCropRect"];
    cropRect = [cropRectValue CGRectValue];

    appDelegate.slectedimage = image; 
    imageView.hidden = YES;

    if( [appDelegate.Name length] != 0 && max_att == 15)
    {
        btnNotDone.hidden   = YES;
        btnDone.enabled = YES;
    }

    //IMAGE SAVE IN DOCUMENTS//////
    [UIImagePNGRepresentation(image) writeToFile:[self findUniqueSavePath] atomically:YES];
    // Show the current contents of the documents folder
    CFShow([[NSFileManager defaultManager] directoryContentsAtPath:[NSHomeDirectory() stringByAppendingString:@"/Documents"]]);




}

请帮助我。我想删除所有警告。

Hi I receive memory warning whenever I am using camera.

The error is like this:

"Receive memory warning..."

And the code is:

-(void) getPhoto{

    GameAppdelegate *appDelegate = (GameAppdelegate *)[[UIApplication sharedApplication]delegate];

    UIImagePickerController * picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;

    ///////////////////////////////////photolibrary//////////////////////////////
    if([appDelegate.photoselection isEqualToString:@"User Pressed Button 1\n"])
    {
        picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;

        if(appDelegate.sound == 1)
        {
            [classObj ButtonSound];
        }
    }
    ///////////////////////////////////Camera//////////////////////////////

    else if([appDelegate.photoselection isEqualToString:@"User Pressed Button 2\n"]) 
    {

        @try 
        {
            picker.sourceType = UIImagePickerControllerSourceTypeCamera;
        }
        @catch (NSException * e)
        {
            UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"ALERT" 
                                                         message:@"Please try again"
                                                        delegate:self 
                                               cancelButtonTitle:nil 
                                               otherButtonTitles:@"ok", nil];
            [av show];

        }
        if(appDelegate.sound == 1)
        {
            [classObj ButtonSound];
        }
    }
    ///////////////////////////////////Cancel//////////////////////////////


    else if([appDelegate.photoselection isEqualToString:@"User Pressed Button 3\n"]) 
    {
        if(appDelegate.sound == 1)
            [classObj ButtonSound];
        return;
    }
    [self presentModalViewController:picker animated:YES];
    [picker release];
}

How can i handle this?please help after taking picture i crop the image and save in application like this:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {

    iRolegameAppDelegate *appDelegate = (iRolegameAppDelegate *)[[UIApplication sharedApplication]delegate];

    if(appDelegate.sound == 1)
    {
        [classObj ButtonSound];
    }

    [picker dismissModalViewControllerAnimated:YES];
    imageView.image = image;
    CGSize size = [imageView.image size];
    CGRect cropRect = CGRectMake(0.0, 0.0, size.width, size.height);

    NSValue *cropRectValue = [editingInfo objectForKey:@"UIImagePickerControllerCropRect"];
    cropRect = [cropRectValue CGRectValue];

    appDelegate.slectedimage = image; 
    imageView.hidden = YES;

    if( [appDelegate.Name length] != 0 && max_att == 15)
    {
        btnNotDone.hidden   = YES;
        btnDone.enabled = YES;
    }

    //IMAGE SAVE IN DOCUMENTS//////
    [UIImagePNGRepresentation(image) writeToFile:[self findUniqueSavePath] atomically:YES];
    // Show the current contents of the documents folder
    CFShow([[NSFileManager defaultManager] directoryContentsAtPath:[NSHomeDirectory() stringByAppendingString:@"/Documents"]]);




}

Please help me. I want to remove all warnings.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

甚是思念 2024-11-15 01:40:51

您正在泄漏 UIImagePickerController。创建时自动释放它或在 dismissModalViewControllerAnimated 之后释放它。

您可能仍然会收到内存警告,照片可能会很大,尤其是在 iPhone 4 上,并且在某个时刻,您的内存中有两个照片:UIImage 和自动释放的 PNG。

PS您似乎没有使用 sizecropRect 所以您可以删除它们。

You're leaking the UIImagePickerController. Autorelease it on creation or release it after dismissModalViewControllerAnimated.

You may still get memory warnings, photos can be enormous, especially on the iPhone 4 and at a certain point you have two of them in memory: the UIImage and the autoreleased PNG.

P.S. you don't seem to be using size and cropRect so you could delete them.

狼亦尘 2024-11-15 01:40:51

释放您的警报视图。一般来说,释放您分配的任何对象。如果您有保留属性,则为其分配一个自动释放对象。

当您收到内存警告时,您的视图控制器方法 - (void)didReceiveMemoryWarning 将被调用。在这里,您必须释放已缓存的所有不需要的对象。通常,这将是一些图像、堆栈中的视图等。

还要检查模态视图控制器中的对象是否有适当的释放。

Release your alert view. In general release any object you alloc. In case you have a retain property then assign a auto release object to it.

When you get a memory warning your view controller method - (void)didReceiveMemoryWarning is called. Here you will have to release any unwanted objects which you have cached. Typically that would be some images, views in stack etc.

Also check if you are having appropriate dealloc for objects in your modal view controller.

往昔成烟 2024-11-15 01:40:51

您是否正在实施 -imagePickerController:didFinishPickingMediaWithInfo: ?您实施的方法已被弃用。即使对于图像,您也应该使用其他方法。你用录制的视频做什么?

顺便说一句,以下代码 -

@try 
{
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
}
@catch (NSException * e)
{
    UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"ALERT" 
                                                message:@"Please try again"
                                               delegate:self 
                                      cancelButtonTitle:nil 
                                      otherButtonTitles:@"ok", nil];
    [av show];
}

应该是

if ( [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] ) {
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
} else {
    UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"ALERT" 
                                                 message:@"Camera isn't available"
                                                delegate:self 
                                       cancelButtonTitle:nil 
                                       otherButtonTitles:@"ok", nil];
    [av show];
    [av release]
}

现在更聪明的事情是禁用按钮 2,这将是

if ( ![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] ) {
    button2.enabled = N0;
}

Are you implementing -imagePickerController:didFinishPickingMediaWithInfo:? The method you've implemented has been deprecated. You should use the other method even for images. What are you doing with the recorded videos?

On a side note, the following code –

@try 
{
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
}
@catch (NSException * e)
{
    UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"ALERT" 
                                                message:@"Please try again"
                                               delegate:self 
                                      cancelButtonTitle:nil 
                                      otherButtonTitles:@"ok", nil];
    [av show];
}

should be

if ( [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] ) {
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
} else {
    UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"ALERT" 
                                                 message:@"Camera isn't available"
                                                delegate:self 
                                       cancelButtonTitle:nil 
                                       otherButtonTitles:@"ok", nil];
    [av show];
    [av release]
}

Now smarter thing would be to disable button 2 which would be

if ( ![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] ) {
    button2.enabled = N0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文