簡體   English   中英

UIImagePickerControllerOriginalImage在iPhone 4中收到內存警告

[英]UIImagePickerControllerOriginalImage received memory warning in iPhone 4

我在右上角有一個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

更新:CAMERA VIEW CONTROLLER 1內存警告和CROP VIEW CONTROLLER內存警告是我在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,因此,它將減少內存警告。

謝謝!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM