我在右上角有一个UICollectionView和一个bar按钮(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语句

最佳答案

让我们尝试这种方法:

-(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,因此,它将减少内存警告。

谢谢!

10-08 12:12