2012-12-18 41 views
1

在我的應用程序中,我試圖使用相機制作單張照片,但它無時無刻都在崩潰。iOS UIImagePickerController iOS 6上的奇怪崩潰

我處理這個問題很久了,所以我提供的代碼,似乎沒有必要:在視圖 - 控制 :

- (id) init 
{ 
    if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) 
    { 
     self = [super initWithNibName:@"WCAddNewWatchViewController_iPhone" bundle:[NSBundle mainBundle]]; 
    }else 
    { 
     self = [super initWithNibName:@"WCAddNewWatchViewController_iPad" bundle:[NSBundle mainBundle]]; 
    } 

    if(self) 
    { 

    } 

    return self; 
} 
- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view from its nib. 

    [[self navigationItem]setRightBarButtonItem:self.AddButton]; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

- (IBAction)ChangeImageButtonTapped:(id)sender { 
    UIImagePickerController* picker = [[UIImagePickerController alloc] init]; 

    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) 
    { 
     picker.sourceType = UIImagePickerControllerSourceTypeCamera; 
    } 
    else 
    { 
     picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 
    } 

    picker.mediaTypes = @[(NSString *) kUTTypeImage]; 
    picker.allowsEditing = NO; 
    [picker setDelegate:self]; 

    [self presentViewController:picker animated:YES completion:nil]; 

} 

#pragma mark UIImagePickerControllerDelegate 

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { 

    NSString *mediaType = info[UIImagePickerControllerMediaType]; 

    [self dismissViewControllerAnimated:YES completion:nil]; 

    if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) { 
     UIImage *image = info[UIImagePickerControllerOriginalImage]; 
     selectedImage = image; 
    // self.watchImageView.image = selectedImage; 

    } 

    NSLog(@"no crash!!!!"); //that's a lie... 
} 

有沒有崩潰的消息,日誌寫道,收到的應用內存警告,然後崩潰。該設備的日誌說:

TIL的外的內存殺手被稱爲「投棄」在iOS

任何人都可以看看我的代碼,並告訴我,我到底做錯了什麼?

+1

總有一個原因。什麼是您的崩潰消息和崩潰日誌? – Bot

+0

沒有崩潰消息,日誌寫道應用程序收到內存警告,然後崩潰。設備日誌說:TIL在iOS上被稱爲「jetsam」的內存不足殺手。 –

+0

有一個crashlog。查看組織者 – Bot

回答

3

你被監視進程殺死了,因爲你沒有處理內存警告 (內部監督辦公室的過程,監控內存使用&應用程序的啓動時間,並殺死出現去流氓應用程序「)

BTW:TIL =今天我瞭解到:D

使用縮小版圖像進行顯示。

- (UIImage *)scaledCopyOfSize:(CGSize)newSize { 
    CGImageRef imgRef = self.CGImage; 

    CGFloat width = CGImageGetWidth(imgRef); 
    CGFloat height = CGImageGetHeight(imgRef); 

    CGAffineTransform transform = CGAffineTransformIdentity; 
    CGRect bounds = CGRectMake(0, 0, width, height); 
    if (width > newSize.width || height > newSize.height) { 
     CGFloat ratio = width/height; 
     if (ratio > 1) { 
      bounds.size.width = newSize.width; 
      bounds.size.height = bounds.size.width/ratio; 
     } 
     else { 
      bounds.size.height = newSize.height; 
      bounds.size.width = bounds.size.height * ratio; 
     } 
    } 

    CGFloat scaleRatio = bounds.size.width/width; 
    CGSize imageSize = CGSizeMake(CGImageGetWidth(imgRef), CGImageGetHeight(imgRef)); 
    CGFloat boundHeight; 
    UIImageOrientation orient = self.imageOrientation; 
    switch(orient) { 

     case UIImageOrientationUp: //EXIF = 1 
      transform = CGAffineTransformIdentity; 
      break; 

     case UIImageOrientationUpMirrored: //EXIF = 2 
      transform = CGAffineTransformMakeTranslation(imageSize.width, 0.0); 
      transform = CGAffineTransformScale(transform, -1.0, 1.0); 
      break; 

     case UIImageOrientationDown: //EXIF = 3 
      transform = CGAffineTransformMakeTranslation(imageSize.width, imageSize.height); 
      transform = CGAffineTransformRotate(transform, M_PI); 
      break; 

     case UIImageOrientationDownMirrored: //EXIF = 4 
      transform = CGAffineTransformMakeTranslation(0.0, imageSize.height); 
      transform = CGAffineTransformScale(transform, 1.0, -1.0); 
      break; 

     case UIImageOrientationLeftMirrored: //EXIF = 5 
      boundHeight = bounds.size.height; 
      bounds.size.height = bounds.size.width; 
      bounds.size.width = boundHeight; 
      transform = CGAffineTransformMakeTranslation(imageSize.height, imageSize.width); 
      transform = CGAffineTransformScale(transform, -1.0, 1.0); 
      transform = CGAffineTransformRotate(transform, 3.0 * M_PI/2.0); 
      break; 

     case UIImageOrientationLeft: //EXIF = 6 
      boundHeight = bounds.size.height; 
      bounds.size.height = bounds.size.width; 
      bounds.size.width = boundHeight; 
      transform = CGAffineTransformMakeTranslation(0.0, imageSize.width); 
      transform = CGAffineTransformRotate(transform, 3.0 * M_PI/2.0); 
      break; 

     case UIImageOrientationRightMirrored: //EXIF = 7 
      boundHeight = bounds.size.height; 
      bounds.size.height = bounds.size.width; 
      bounds.size.width = boundHeight; 
      transform = CGAffineTransformMakeScale(-1.0, 1.0); 
      transform = CGAffineTransformRotate(transform, M_PI/2.0); 
      break; 

     case UIImageOrientationRight: //EXIF = 8 
      boundHeight = bounds.size.height; 
      bounds.size.height = bounds.size.width; 
      bounds.size.width = boundHeight; 
      transform = CGAffineTransformMakeTranslation(imageSize.height, 0.0); 
      transform = CGAffineTransformRotate(transform, M_PI/2.0); 
      break; 

     default: 
      [NSException raise:NSInternalInconsistencyException format:@"Invalid image orientation"]; 

    } 

    if (UIGraphicsBeginImageContextWithOptions) { 
     UIGraphicsBeginImageContextWithOptions(bounds.size, NO, 
               /* 0.0f will scale to 1.0/2.0 depending on if the 
               device has a high-resolution screen */ 
               0.0f); 
    } else { 
     UIGraphicsBeginImageContext(bounds.size); 
    } 

    CGContextRef context = UIGraphicsGetCurrentContext(); 

    if (orient == UIImageOrientationRight || orient == UIImageOrientationLeft) { 
     CGContextScaleCTM(context, -scaleRatio, scaleRatio); 
     CGContextTranslateCTM(context, -height, 0); 
    } 
    else { 
     CGContextScaleCTM(context, scaleRatio, -scaleRatio); 
     CGContextTranslateCTM(context, 0, -height); 
    } 

    CGContextConcatCTM(context, transform); 

    CGContextDrawImage(context, CGRectMake(0, 0, width, height), imgRef); 
    UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    return imageCopy; 
} 
+0

我已經試過了,但沒有奏效。你能提供一些代碼嗎? –

+0

我確實提供了比例方法 –

+0

這解決了我的問題!非常好!謝謝! –