【问题标题】:iOS UIImagePickerController strange crash on ios 6ios 6上的iOS UIImagePickerController奇怪崩溃
【发布时间】:2012-12-18 16:21:10
【问题描述】:

在我的应用程序中,我尝试使用相机制作单张照片,但它总是无缘无故地崩溃。

我已经处理这个问题很长时间了,所以我提供了一些看似不必要的代码: 在视图控制器中:

- (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...
}

没有崩溃消息,日志写道应用收到内存警告,然后崩溃。设备日志说:

直到内存不足的杀手在 iOS 上被称为“jetsam”

谁能看看我的代码并告诉我我做错了什么?

【问题讨论】:

  • 总是有原因的。您的崩溃消息和崩溃日志是什么?
  • 没有崩溃消息,日志写道应用收到内存警告,然后崩溃。设备日志显示:TIL 内存不足杀手在 iOS 上称为“jetsam”。
  • 有一个崩溃日志。看看组织者
  • WatchCollectorNavController[34790] :收到内存警告。 com.apple.launchd[1] (com.apple.networkd_privileged[34742]) : (com.apple.networkd_privileged) Exited: Killed: 9 ReportCrash[34792] : libMobileGestalt copySystemVersionDictionaryValue: 无法从中查找 ReleaseType系统版本字典 ReportCrash[34792] :不保存仅暂停的 Jetsam 日志,因为今天已经转储。 UserEventAgent[13] : jetsam: 正在创建内核终止快照 SpringBoard[69] : 收到内存警告。
  • 单击产品 -> 编辑方案 -> 诊断,在内存管理部分中 Enable Guard MallocEnable Zombie Objects 的复选框,这可能会给你一些东西

标签: ios uiimagepickercontroller


【解决方案1】:

你被看门狗进程杀死,因为你没有处理内存警告 (ios 具有监控内存使用情况和应用启动时间的进程,并会杀死看似流氓的应用)

顺便说一句: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;
}

【讨论】:

  • 我已经尝试过了,但没有奏效。你能提供一些代码吗?
  • 我确实提供了一个缩放方法
猜你喜欢
  • 2012-10-09
  • 1970-01-01
  • 2011-12-10
  • 2011-07-14
  • 1970-01-01
  • 2023-03-25
  • 2012-01-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多