【发布时间】:2014-03-17 12:02:57
【问题描述】:
我在右上角有一个 UICollectionView 和一个条形按钮(CameraViewController1 : UICollectionViewController)。流程是当我拍照时,它移动到一个新的视图控制器,可以裁剪图像。用户有两个选项使用和取消之后选择此选项中的任何一个,它都会将图像返回到集合视图并像单元格一样排列。我想拍很多照片。但我最多可以拍 3 张照片然后应用程序崩溃
这种情况只发生在 iPhone 4 上,而不是 iPhone 5 上。
为什么应用程序只在 iPhone 4 上崩溃?相同的代码在 iPhone 5 中完美运行。
当我在图像视图中回顾时,我想要一个良好的图像质量使用UIImagePickerControllerOriginalImage 行给我提供了良好的质量,但它在 iPhone 4 中会崩溃请任何解决方案!
在这门课上我应该做些什么来消除这个内存警告(iPhone 4 中的原因)?
我尝试调整图像大小,但它给我的图像质量很差。即使调整大小后也可以获得高质量的图像吗?
请帮忙!如何摆脱这个内存警告?
这里是完整的代码:
- (void)viewDidLoad
{
[[UIApplication sharedApplication]setStatusBarHidden:FALSE withAnimation:NO];
[barbutton setTitleTextAttributes:@{
UITextAttributeFont: [UIFont fontWithName:@"Arial" size:20.0],
UITextAttributeTextColor: [UIColor blueColor]
} forState:UIControlStateNormal];
hide_backbtn=1;
if ([[[UIDevice currentDevice] systemVersion] floatValue]>=7.0)
{
[self.navigationController.navigationBar setTintColor:[UIColor blueColor]];
[[UIBarButtonItem appearance]setTintColor:[UIColor blueColor]];
}
else
{
[self.navigationController.navigationBar setTintColor:[UIColor blackColor]];
[[UIBarButtonItem appearance]setTintColor:[UIColor blackColor]];
}
//self.collection_view.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"Bottom.png"]];
self.collectionView.backgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Bottom.png"]]autorelease];//ar
[super viewDidLoad];
crop_one.hidden=TRUE;
view_refresh=3;
selectedRecipes = [NSMutableArray array];
//noww imgSmall=[[[UIImage alloc]init]autorelease];//ar
picker1 = [[UIImagePickerController alloc] init];
picker1.delegate = self;
if (! (picker1==nil)) {
NSLog(@"YUCCKKK");
}
if ((picker1==nil)&&(original_image < 0)) {
NSLog(@"YEEEEEEYYYYYYYYY");
}
// Do any additional setup after loading the view.
}
- (IBAction)TakeaPhoto:(id)sender {
[[UIApplication sharedApplication]setStatusBarHidden:FALSE withAnimation:NO];
gallery=0;
picker1.sourceType = UIImagePickerControllerSourceTypeCamera;
self.resizeableCropArea =YES;
self.cropSize=CGSizeMake(300,350);//296, 350
// picker1.allowsEditing = YES;
[self presentViewController:picker1 animated:YES completion:NULL];
[self.picker1 release];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[self dismissViewControllerAnimated:YES completion:NULL];
UIImage *image =[info objectForKey:UIImagePickerControllerOriginalImage] ;
CGSize sizeCropped = CGSizeMake(600,800);//you can give any size 320, 370
// UIImage *theimage = nil;
image = [self resizeImage:image newSize:sizeCropped];
CropViewController *cropController = [[CropViewController alloc] init];
cropController.sourceImage=image;
cropController.sourceImage = [info objectForKey:UIImagePickerControllerOriginalImage]; //now
Original_img = UIImageJPEGRepresentation(cropController.sourceImage,0.0); ///////now
[original_image addObject:[UIImage imageWithData:Original_img]]; // nowwww
NSLog(@"source image=%@",cropController.sourceImage);
cropController.resizeableCropArea = self.resizeableCropArea;
cropController.cropSize = self.cropSize;
cropController.delegate = self;
Cancel_Image= cropController.sourceImage;
[self.navigationController
pushViewController:cropController animated:YES];
[cropController release];
}
- (UIImage *)resizeImage:(UIImage*)image newSize:(CGSize)newSize {
CGRect newRect = CGRectIntegral(CGRectMake(0, 0, newSize.width, newSize.height));
CGImageRef imageRef = image.CGImage;
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
// Set the quality level to use when rescaling
CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
CGAffineTransform flipVertical = CGAffineTransformMake(1, 0, 0, -1, 0, newSize.height);
CGContextConcatCTM(context, flipVertical);
// Draw into the context; this scales the image
CGContextDrawImage(context, newRect, imageRef);
// Get the resized image from the context and a UIImage
CGImageRef newImageRef = CGBitmapContextCreateImage(context);
UIImage *newImage = [UIImage imageWithCGImage:newImageRef];
CGImageRelease(newImageRef);
UIGraphicsEndImageContext();
return newImage;
}
//- (UIImage*)imageTemp:(UIImage*)image scaledToSize:(CGSize)newSize
//{
//
// UIGraphicsBeginImageContext( newSize );
// [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];//newSize.width,newSize.height)
//
// UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
// UIGraphicsEndImageContext();
//
// return newImage;
//
//
//
//
//}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker1 dismissViewControllerAnimated:YES completion:NULL];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return [image_name count];
// [self.collection_view cellForItemAtIndexPath:];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"Cell";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
int row = indexPath.row;
recipeImageView = (UIImageView *)[cell viewWithTag:100];
//UIImage *img = [UIImage imageWithData:imageData];
//UIImage *image = [[UIImage alloc] initWithData:imageData];
//[recipeImageView setImage:image];
//recipeImageView.image = image_cap;
recipeImageView.image = (UIImage*) [image_name objectAtIndex:row];
// cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"img.png"]];
//cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Bottom.png"]];
cell.backgroundColor=[UIColor blackColor];
return cell;
}
-(void)sample
{
// NSLog(@"%@",destViewController.recipeImageName);
}
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
NSString *deSelectedRecipe = [image_name objectAtIndex:indexPath.row];
[selectedRecipes removeObject:deSelectedRecipe];
}
- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender
{
return YES;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
mt_img=1;
NSArray *indexPaths = [self.collectionView indexPathsForSelectedItems];
hide_backbtn=1;
NSIndexPath *indexPath1 = [indexPaths objectAtIndex:0];
recipeImageName = [image_name objectAtIndex:indexPath.row];
if(appDelegate.intAddOrView==2)
{
hide_backbtn=20;
}
selected_image=indexPath.row;
NSLog(@"%@",recipeImageName);
[self.collectionView deselectItemAtIndexPath:indexPath1 animated:NO];
[arrInsertReceipt replaceObjectAtIndex:0 withObject: [image_name objectAtIndex:indexPath.row]];
[self.navigationController popViewControllerAnimated:YES]; //now
}
- (void)singleTapGestureCaptured:(UITapGestureRecognizer *)gesture
{
// UIView *tappedView = [gesture.view hitTest:[gesture locationInView:gesture.view] withEvent:nil];
NSLog(@"Touch event ");
CGPoint point = [gesture locationInView:self.collectionView];
NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:point];
if (indexPath)
{
NSLog(@"Image was double tapped");
}
}
- (void)viewWillAppear:(BOOL)animated
{
[[UIApplication sharedApplication]setStatusBarHidden:FALSE withAnimation:NO];
[[self navigationController] setNavigationBarHidden:NO animated:YES];
[barbutton setTitleTextAttributes:@{
UITextAttributeFont: [UIFont fontWithName:@"Arial" size:20.0],
UITextAttributeTextColor: [UIColor blueColor]
} forState:UIControlStateNormal];
if ([[[UIDevice currentDevice] systemVersion] floatValue]>=7.0)
{
[self.navigationController.navigationBar setTintColor:[UIColor blueColor]];
[[UIBarButtonItem appearance]setTintColor:[UIColor blueColor]];
}
else
{
[self.navigationController.navigationBar setTintColor:[UIColor blackColor]];
[[UIBarButtonItem appearance]setTintColor:[UIColor blackColor]];
}
// [self viewDidLoad];
[collection_view reloadData];
}
- (void)didReceiveMemoryWarning
{
// [localarray removeAllObjects];
[super didReceiveMemoryWarning];
NSLog(@"CAMERA VIEW CONTROLLER 1 MEMORY WARNING");
// NSLog(@"HERERERERER");
// Dispose of any resources that can be recreated.
}
-(void)viewDidDisappear:(BOOL)animated{
[original_image removeAllObjects];
recipeImageView.image=nil;
}
@end
更新:
相机视图控制器 1 内存警告和裁剪视图控制器内存警告
是我保存在didReceiveMemoryWarning 下的 NSLog 语句
【问题讨论】:
-
两种设备的操作系统版本是多少?你能显示崩溃日志吗?
-
@Mani iPhone 4 = 7.0.2 iPhone 5 = 7.0.4,也更新了我的问题。
标签: ios iphone objective-c uiimagepickercontroller didreceivememorywarning