我右上角有一个UICollectionView和一个条形按钮(CameraViewController1:UICollectionViewController)。当我拍照时它会移动到一个新的视图控制器,在那里可以裁剪图像。用户有两个选项Use and Cancel after选择任何一个选项,它将图像返回到集合视图,它像细胞一样排列。我想拍很多照片。但我最多可以拍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
答案 0 :(得分:1)
我们试试这个方法:
-(UIImage *)getResizedImageForImage:(UIImage *)originalImage forSize:(CGSize)size
{
@autoreleasepool
{
UIGraphicsBeginImageContext(size);
[originalImage drawInRect:CGRectMake(0.0, 0.0, size.width, size.height) blendMode:kCGBlendModeNormal alpha:1.0];
UIImage * finalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return finalImage;
}
}
这里我们使用了@autoreleasepool,因此,它将减少内存警告。
谢谢!