当我想在图像库中选择图像时,iOS模拟器或iPhone的屏幕会冻结

时间:2012-05-05 13:03:52

标签: ios5 xcode4.2 ios-simulator uiimagepickercontroller freeze

我在xos 4.2上为ios 5工作,我在iphone 4S上测试很少的应用程序。 ARC,没有故事板。 我尝试创建一个学习的应用程序,所以我用第一个视图菜单测试一个应用程序(xib文件只有一个按钮,下载第二个视图,HHKMenuViewController.h和.m),第二个视图(带有UIImage的xib文件和两个按钮,HHKListViewController.h和.m)。第二种观点是,用户可以在图像库中获取图像(第一个按钮)或返回第一个视图菜单(第二个按钮)。

那是工作。没有错误。我在iOS模拟器上编译,我有出现的菜单。当我点击按钮时,我进入第二个视图,当我点击“选择图片”按钮时我可以选择我的图片,这张图片出现在第二个视图中图像的框架中。一切都还可以。

但是,当我第一次观看时 - >第二个视图(点击按钮返回) - >第一次观看...大约10次,当我想进入图像库iOS模拟器冻结(Xcode没有错误),我什么也做不了。这在我的iPhone 4s上是一样的。我尝试用仪器(开发人员工具)查看内存的活动,但我发现与其他应用程序没有任何关系。

你有什么建议吗?

感谢您的帮助。

----------- HHKAppDelegate.h ------------------

    #import <UIKit/UIKit.h>
@class HHKMenuViewController;

@interface HHKAppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) HHKMenuViewController *menuViewController;

@end

----------- HHKAppDelegate.m -------------------

    #import "HHKAppDelegate.h"
#import "HHKMenuViewController.h"

@implementation HHKAppDelegate

@synthesize window = _window;
@synthesize menuViewController;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen]applicationFrame]];
    // Override point for customization after application launch
    self.menuViewController = [[HHKMenuViewController alloc] initWithNibName:@"HHKMenuViewController" bundle:nil];
    UIView *menuView = self.menuViewController.view;
    [self.window addSubview:menuView];   
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}
@end

--------- HHKMenuViewController.h ---------------

#import <UIKit/UIKit.h>
#import <mach/mach.h>
@class HHKListViewController;
@class HHKMenuViewController;

@interface HHKMenuViewController : UIViewController

@property (strong, nonatomic) HHKListViewController *listViewController;
@property (strong, nonatomic) HHKMenuViewController *menuViewController;

- (IBAction)listButtonPressed:(id)sender;

@end

-------- HHKMenuViewController.m ----------------

#import "HHKMenuViewController.h"
#import "HHKListViewController.h"

@implementation HHKMenuViewController

@synthesize listViewController;
@synthesize menuViewController;

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    self.listViewController = nil;}

#pragma mark - View lifecycle

- (void)viewDidUnload
{
    [super viewDidUnload];
    self.listViewController = nil;}

- (IBAction)listButtonPressed:(id)sender {

self.menuViewController = [[HHKMenuViewController alloc] initWithNibName:@"HHKMenuViewController" bundle:nil];
UIView *menuView = self.menuViewController.view; 

self.listViewController = [[HHKListViewController alloc] initWithNibName:@"HHKListViewController" bundle:nil];
UIView *listView = self.listViewController.view;

[UIView transitionWithView:self.view duration:0.75 options:UIViewAnimationOptionTransitionCurlUp
animations:^{[menuView removeFromSuperview];[self.view insertSubview:listView aboveSubview:menuView];} 
                completion:NULL];}
@end

---------- HHKListViewController.h -----------------

#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>

@class HHKMenuViewController;
@class HHKListViewController;

@interface HHKListViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>

@property (strong, nonatomic) HHKMenuViewController *menuViewController;
@property (strong, nonatomic) HHKListViewController *listViewController;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (strong, nonatomic) UIImage *image;
@property (copy, nonatomic) NSString *lastChosenMediaType;
@property (assign, nonatomic) CGRect imageFrame;

-(IBAction)backMenuList:(id)sender;
-(IBAction)selectExistingPicture:(id)sender;

@end

---------- HHKListViewController.m ------------------

#import "HHKListViewController.h"
#import "HHKMenuViewController.h"
#import <MobileCoreServices/UTCoreTypes.h>
#import <mach/mach.h>

@interface HHKListViewController ()
static UIImage *shrinkImage(UIImage *original, CGSize size);
-(void)updateDisplay;
-(void)getMediaFromSource:(UIImagePickerControllerSourceType)sourceType;
@end

@implementation HHKListViewController

@synthesize menuViewController;
@synthesize listViewController;
@synthesize imageView;
@synthesize image;
@synthesize lastChosenMediaType;
@synthesize imageFrame;

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    imageFrame = imageView.frame;}

-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self updateDisplay];}

- (void)viewDidUnload
{
    [super viewDidUnload];
    self.imageView = nil;
    self.listViewController = nil;}

-(IBAction)backMenuList:(id)sender {

    self.menuViewController = [[HHKMenuViewController alloc] initWithNibName:@"HHKMenuViewController" bundle:nil];
    UIView *menuView = self.menuViewController.view; 
    self.listViewController = [[HHKListViewController alloc] initWithNibName:@"HHKListViewController" bundle:nil];
    UIView *listView = self.listViewController.view;
    [UIView transitionWithView:self.view duration:0.75 options:UIViewAnimationOptionTransitionCurlDown
                    animations:^{
                        [listView removeFromSuperview];
                        [self.view insertSubview:menuView aboveSubview:listView];
                    } 
                    completion:NULL];}

- (IBAction)selectExistingPicture:(id)sender {

    [self getMediaFromSource:UIImagePickerControllerSourceTypePhotoLibrary];}

/*---------------------------------------------------------------*/
/*--------------------------------------------------------------*/
#pragma mark UIImagePickerController delegate methods

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

        UIImage *chosenImage = [info objectForKey:UIImagePickerControllerEditedImage];
        UIImage *shrunkenImage = shrinkImage(chosenImage, imageFrame.size);
        self.image = shrunkenImage;
     [picker dismissModalViewControllerAnimated: YES]; }


-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{

    [picker dismissModalViewControllerAnimated:YES];}

#pragma mark -

static UIImage *shrinkImage(UIImage *original, CGSize size) {
    CGFloat scale = [UIScreen mainScreen].scale;
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate(NULL, size.width * scale,size.height * scale, 8, 0, colorSpace, kCGImageAlphaPremultipliedFirst);
    CGContextDrawImage(context,CGRectMake(0, 0, size.width * scale, size.height * scale),original.CGImage);
    CGImageRef shrunken = CGBitmapContextCreateImage(context);
    UIImage *final = [UIImage imageWithCGImage:shrunken];
    CGContextRelease(context);
    CGImageRelease(shrunken);   
    return final;}

-(void)updateDisplay {
    imageView.image = image;
    imageView.hidden = NO;}

- (void)getMediaFromSource:(UIImagePickerControllerSourceType)sourceType {

     NSArray *mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:sourceType];
    if ([UIImagePickerController isSourceTypeAvailable:sourceType] && [mediaTypes count] > 0) {
        NSArray *mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:sourceType];
        UIImagePickerController *picker =[[UIImagePickerController alloc] init];
        picker.mediaTypes = mediaTypes;
        picker.delegate = self;
        picker.allowsEditing = YES;
        picker.sourceType = sourceType;
        [self presentModalViewController:picker animated:YES];
    } else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error accessing media" message:@"Device doesn’t support that media source." 
                              delegate:nil cancelButtonTitle:@"Drat!" otherButtonTitles:nil];
        [alert show];}}

@end

1 个答案:

答案 0 :(得分:0)

问题发生在appdelegate.m

#import "HHKAppDelegate.h"
#import "HHKMenuViewController.h"

@implementation HHKAppDelegate

@synthesize window = _window;
@synthesize navigationController = _navigationController;


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:      (NSDictionary *)launchOptions
{
[[UIApplication sharedApplication] setStatusBarHidden:YES   withAnimation:UIStatusBarAnimationFade];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
HHKMenuViewController *menuViewController = [[HHKMenuViewController alloc]   initWithNibName:@"HHKMenuViewController" bundle:nil];
self.navigationController = [[UINavigationController alloc]   initWithRootViewController:menuViewController];
self.window.rootViewController = self.navigationController;
navigationController.navigationBarHidden = YES;
[self.window makeKeyAndVisible];
return YES;
}