程序收到 uiimagepickercontroller 的信号 0

发布于 2024-11-16 14:18:51 字数 1694 浏览 1 评论 0原文

我有一个应用程序,用户可以从相机拍照,图像显示在图像视图中。 第一次工作正常,但第二次应用程序崩溃,留下内存警告级别=1

我用谷歌搜索并尝试了所有可能的方法,但没有用

下面是我在应用程序中使用的代码

       UIImagePickerController *imgpicker;

        @property(nonatomic,retain) UIImagePickerController *imgpicker;



        @synthesize imgpicker;  

        if(actionSheet.tag==1)
            {
                NSLog(@"button index:%i",buttonIndex);
                if(buttonIndex == 0)
                { NSLog(@"button index for camera:%i",buttonIndex);
                    if(self.imgpicker==nil){
                    self.imgpicker=[[UIImagePickerController alloc]init];

                    self.imgpicker.delegate =self;
                    self.imgpicker.sourceType=UIImagePickerControllerSourceTypeCamera;

                    }

                    [self presentModalViewController:self.imgpicker animated:YES];


                    [imgpicker release];
                    return;

                }
}

        - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
        {
           NSLog(@"didFinishPickingMediaWithInfo editing info:%@",info);
            UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];



            imageview = [[UIImageView alloc] initWithFrame:CGRectMake(65, 10, 75, 75)];



                NSData *newjpg = UIImageJPEGRepresentation(image, 0.5);



            [imageview setImage:[UIImage imageWithData:newjpg]];



            [scrollView addSubview:imageview];



            [self.view addSubview:scrollView];

            [[picker parentViewController] dismissModalViewControllerAnimated:YES];
    }

请帮助我

I have an app where user can take picture from camera and image is displayed in imageview.
It works fine for the first time but the second time app is crashed leaving memory warning level=1

I google around and tried all possible ways but no use

Below is the code i used in my app

       UIImagePickerController *imgpicker;

        @property(nonatomic,retain) UIImagePickerController *imgpicker;



        @synthesize imgpicker;  

        if(actionSheet.tag==1)
            {
                NSLog(@"button index:%i",buttonIndex);
                if(buttonIndex == 0)
                { NSLog(@"button index for camera:%i",buttonIndex);
                    if(self.imgpicker==nil){
                    self.imgpicker=[[UIImagePickerController alloc]init];

                    self.imgpicker.delegate =self;
                    self.imgpicker.sourceType=UIImagePickerControllerSourceTypeCamera;

                    }

                    [self presentModalViewController:self.imgpicker animated:YES];


                    [imgpicker release];
                    return;

                }
}

        - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
        {
           NSLog(@"didFinishPickingMediaWithInfo editing info:%@",info);
            UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];



            imageview = [[UIImageView alloc] initWithFrame:CGRectMake(65, 10, 75, 75)];



                NSData *newjpg = UIImageJPEGRepresentation(image, 0.5);



            [imageview setImage:[UIImage imageWithData:newjpg]];



            [scrollView addSubview:imageview];



            [self.view addSubview:scrollView];

            [[picker parentViewController] dismissModalViewControllerAnimated:YES];
    }

Please help me

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

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

发布评论

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

评论(3

墨小墨 2024-11-23 14:18:51

尝试将其更改

self.imgpicker=[[UIImagePickerController alloc]init];

self.imgpicker=[[[UIImagePickerController alloc]init] autorelease];

:并删除此行:

[imgpicker release];

如果多次显示图像选择器,则每次选择器完成时分配的图像视图都会泄漏内存。
您每次还将图像视图添加到滚动视图,并将滚动视图添加到视图控制器的视图。您只需执行一次,然后更新图像视图上的 image 属性。

试试这个:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    NSLog(@"didFinishPickingMediaWithInfo editing info:%@",info);
    UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];

    if (imageview == nil) {
        imageview = [[UIImageView alloc] initWithFrame:CGRectMake(65, 10, 75, 75)];
        [scrollView addSubview:imageview];
        [self.view addSubview:scrollView];
    }

    NSData *newjpg = UIImageJPEGRepresentation(image, 0.5);

    [imageview setImage:[UIImage imageWithData:newjpg]];

    [[picker parentViewController] dismissModalViewControllerAnimated:YES];
}

Try changing this:

self.imgpicker=[[UIImagePickerController alloc]init];

to this:

self.imgpicker=[[[UIImagePickerController alloc]init] autorelease];

and remove this line:

[imgpicker release];

If you're showing the image picker more than once, you're leaking memory from the image view that gets allocated every time the picker finishes.
You're also adding the image view to the scroll view and the scroll view to the view controller's view every time. You just need to do it once, and the update the image property on the image view.

Try this:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    NSLog(@"didFinishPickingMediaWithInfo editing info:%@",info);
    UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];

    if (imageview == nil) {
        imageview = [[UIImageView alloc] initWithFrame:CGRectMake(65, 10, 75, 75)];
        [scrollView addSubview:imageview];
        [self.view addSubview:scrollView];
    }

    NSData *newjpg = UIImageJPEGRepresentation(image, 0.5);

    [imageview setImage:[UIImage imageWithData:newjpg]];

    [[picker parentViewController] dismissModalViewControllerAnimated:YES];
}
倾城°AllureLove 2024-11-23 14:18:51

请在 AppDelegate 类中创建 UIImagePickerController 对象,并在 AppDidFinishloading 方法中进行初始化:didFinishLaunchingWithOptions。在 appDelegate dealloc 方法上实现它。
您可以从 appdelegate 类在所需的视图控制器页面中访问它。
请不要在需要时创建任何时候的 UIImagePickerController 对象并实现它。

这可能对你有帮助。

Please create UIImagePickerController object in your AppDelegate class and initialize in your AppDidFinishloading method : didFinishLaunchingWithOptions. Realese it on appDelegate dealloc method.
You can access it in your needed view controller Page from appdelegate class.
Please do not create evertytime UIImagePickerController object when u needed and realese it.

This might help you.

空名 2024-11-23 14:18:51

请使用此代码:

App Delegate
// .h

#import <UIKit/UIKit.h>

@interface MyProjectAppDelegate : NSObject <UIApplicationDelegate> {

    UIImagePickerController *image_picker;

    UIWindow *window;
    UINavigationController *navigationController;
}
@property(nonatomic, retain) UIImagePickerController *image_picker;
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;

-(void)initializeImagePickerView;

@end

// .m

#import "MyProjectAppDelegate.h"
#import "RootViewController.h"

@implementation MyProjectAppDelegate

@synthesize window;
@synthesize navigationController,image_picker;

#pragma mark -
#pragma mark Application lifecycle

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    // Override point for customization after application launch.
    [self initializeImagePickerView];

    // Set the navigation controller as the window's root view controller and display.
    self.window.rootViewController = self.navigationController;
    [self.window makeKeyAndVisible];

    return YES;
}

-(void)initializeImagePickerView
{
    // Picker View
    self.image_picker = [[UIImagePickerController alloc] init];
    self.image_picker.allowsImageEditing = YES;
}

- (void)dealloc {
    [image_picker release];
    [navigationController release];
    [window release];
    [super dealloc];
}
@end

//RootView 控制器
// .h

#import <UIKit/UIKit.h>
#import "MyProjectAppDelegate.h"

@interface RootViewController : UIViewController <UIImagePickerControllerDelegate,UINavigationControllerDelegate>{

    MyProjectAppDelegate *appDelegate;
}
-(IBAction)btnPhotoPressed:(id)sender;
@end

// .m

#import "RootViewController.h"
@implementation RootViewController

#pragma mark -
#pragma mark View lifecycle
- (void)viewDidLoad {
    [super viewDidLoad];

    appDelegate = (MyProjectAppDelegate *)[[UIApplication sharedApplication] delegate];
}

-(IBAction)btnPhotoPressed:(id)sender
{
    appDelegate.image_picker.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
    appDelegate.image_picker.delegate=self;
    [self presentModalViewController:appDelegate.image_picker animated:YES];
}

#pragma mark UIImagePickerController delegate
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    // Access the uncropped image from info dictionary
    UIImage *imgCapturePhoto = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

    // You can use image here

    [self dismissModalViewControllerAnimated:YES];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *) picker
{
    [picker dismissModalViewControllerAnimated:YES];
}
- (void)dealloc {
    [super dealloc];
}

这肯定会对你有帮助。

Please use this code :

App Delegate
// .h

#import <UIKit/UIKit.h>

@interface MyProjectAppDelegate : NSObject <UIApplicationDelegate> {

    UIImagePickerController *image_picker;

    UIWindow *window;
    UINavigationController *navigationController;
}
@property(nonatomic, retain) UIImagePickerController *image_picker;
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;

-(void)initializeImagePickerView;

@end

// .m

#import "MyProjectAppDelegate.h"
#import "RootViewController.h"

@implementation MyProjectAppDelegate

@synthesize window;
@synthesize navigationController,image_picker;

#pragma mark -
#pragma mark Application lifecycle

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    // Override point for customization after application launch.
    [self initializeImagePickerView];

    // Set the navigation controller as the window's root view controller and display.
    self.window.rootViewController = self.navigationController;
    [self.window makeKeyAndVisible];

    return YES;
}

-(void)initializeImagePickerView
{
    // Picker View
    self.image_picker = [[UIImagePickerController alloc] init];
    self.image_picker.allowsImageEditing = YES;
}

- (void)dealloc {
    [image_picker release];
    [navigationController release];
    [window release];
    [super dealloc];
}
@end

//RootView Controller
// .h

#import <UIKit/UIKit.h>
#import "MyProjectAppDelegate.h"

@interface RootViewController : UIViewController <UIImagePickerControllerDelegate,UINavigationControllerDelegate>{

    MyProjectAppDelegate *appDelegate;
}
-(IBAction)btnPhotoPressed:(id)sender;
@end

// .m

#import "RootViewController.h"
@implementation RootViewController

#pragma mark -
#pragma mark View lifecycle
- (void)viewDidLoad {
    [super viewDidLoad];

    appDelegate = (MyProjectAppDelegate *)[[UIApplication sharedApplication] delegate];
}

-(IBAction)btnPhotoPressed:(id)sender
{
    appDelegate.image_picker.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
    appDelegate.image_picker.delegate=self;
    [self presentModalViewController:appDelegate.image_picker animated:YES];
}

#pragma mark UIImagePickerController delegate
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    // Access the uncropped image from info dictionary
    UIImage *imgCapturePhoto = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

    // You can use image here

    [self dismissModalViewControllerAnimated:YES];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *) picker
{
    [picker dismissModalViewControllerAnimated:YES];
}
- (void)dealloc {
    [super dealloc];
}

This will definitely help u.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文