我使用以下代码段打开相机,点击照片并使用照片(当出现两个选项重新拍摄和使用照片时)。
- (IBAction)openCamera
{
@autoreleasepool {
[imageSpinner stopAnimating];
[imageSpinner removeFromSuperview];
[cameralabel setText:@""];
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
UIImagePickerController *imagePicker =
[[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType =
UIImagePickerControllerSourceTypeCamera;
imagePicker.allowsEditing = NO;
[self presentViewController:imagePicker animated:NO completion:nil];
imagePicker=nil;
}
}
}
-(void)setImageSpinner
{
@autoreleasepool {
imageSpinner = nil;
imageSpinner = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
imageSpinner.center = self.view.center;
[imageSpinner startAnimating];
[self.view addSubview:imageSpinner];
[self.view bringSubviewToFront:imageSpinner];
}
}
- (void)viewDidLoad
{
[self openCamera];
[super viewDidLoad];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.navigationController setToolbarHidden:YES];
self.navigationItem.hidesBackButton = NO;
}
-(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
-(void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSString *savedImagePath;
@autoreleasepool {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
savedImagePath = [documentsDirectory stringByAppendingPathComponent:@"Image 1.jpeg"];
paths=nil;
documentsDirectory=nil;
[picker dismissViewControllerAnimated:NO completion:NULL];
[cameralabel setText:@"Please wait"];
[self setImageSpinner];
}
@autoreleasepool {
ManifestDocumentViewController *manifestVC = [self.storyboard instantiateViewControllerWithIdentifier:@"manifestImage"];
manifestVC.getTempFilePath = savedImagePath;
if ([screenName isEqualToString:@"Retake"])
{
manifestVC.screenName = @"Retake";
manifestVC.retakeFileID = retakeFileID;
manifestVC.retakeFilePath = retakeFilePath;
NSData *data = [NSData dataWithContentsOfFile:savedImagePath];
[data writeToFile:retakeFilePath atomically:YES];
data=nil;
}
else
{
manifestVC.screenName = @"Camera";
UIImage *image = info[UIImagePickerControllerOriginalImage];
NSData *imageData = [NSData dataWithData:UIImageJPEGRepresentation(image, 1.0f)];
[imageData writeToFile:savedImagePath atomically:NO];
image=nil;
imageData=nil;
}
savedImagePath=nil;
[self.navigationController pushViewController:manifestVC animated:YES];
}
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker dismissViewControllerAnimated:YES completion:NULL];
picker.delegate=nil;
picker=nil;
}
- (UIStatusBarStyle)preferredStatusBarStyle
{
return UIStatusBarStyleLightContent;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
[[NSURLCache sharedURLCache] removeAllCachedResponses];
}
在iPhone 4s上的调试会话期间,内存会突然增加,如调试导航器下的内存使用情况所示。附带的是Memory Usage比较的屏幕截图。 “其他流程”的加薪是一个值得关注的问题。
收到的内存警告也会出现在显示的日志中。在运行代码多次后应用程序崩溃(消息“丢失与iPhone的连接”)。这相当于内存中的峰值和完全消耗的“Free”内存。
可以尽可能优化上述代码,以确保尽可能减少内存使用量。