我有一个应用程序,用户可以从相机拍照,图像显示在imageview中。 它第一次工作正常,但第二次应用程序崩溃,留下内存警告级别= 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];
}
请帮帮我
答案 0 :(得分:1)
尝试更改此内容:
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];
}
答案 1 :(得分:0)
请在AppDelegate类中创建UIImagePickerController对象,并在AppDidFinishloading方法中初始化:didFinishLaunchingWithOptions。在appDelegate dealloc方法上重新发布它。 您可以从appdelegate类在所需的视图控制器页面中访问它。 请不要在需要时创建evertytime UIImagePickerController对象并将其重新定位。
这可能会对你有所帮助。
答案 2 :(得分:0)
请使用此代码:
App代表 // .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];
}
这肯定会对你有所帮助。