我为 ios 5 开发 xcode 4.2,并在 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