简体   繁体   English

iOS UIImagePickerController在ios 6上出现奇怪的崩溃

[英]iOS UIImagePickerController strange crash on ios 6

In my app i'm trying to make a single picture using the camera, but it crashes all the time for no reason. 在我的应用程序中,我正在尝试使用相机制作单张照片,但它无缘无故地崩溃。

I'm dealing with this problem for long now, so i'm providing code that can seem unnecessary: in the viewcontroller: 我现在处理这个问题很久了,所以我提供的代码似乎是不必要的:在viewcontroller中:

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

There's no crash message, the log writes that the app received memory warnings, then crashes. 没有崩溃消息,日志写道应用程序收到内存警告,然后崩溃。 The device log says: 设备日志说:

TIL the out-of-memory killer is called "jetsam" on iOS TIL内存不足的杀手在iOS上被称为“jetsam”

Can anyone look at my code and tell me what am I doing wrong? 任何人都可以看看我的代码并告诉我我做错了什么?

you got killed by the watchdog process because you didnt handle memory warnings (ios has process that monitors mem usage & app startup time and kills apps that appear to go rogue') 你被监视程序进程杀了,因为你没有处理内存警告(ios有监视内存使用和应用程序启动时间并杀死看似流氓的应用程序的进程)

btw: TIL = Today I learned :D 顺便说一句:TIL =今天我学到了:D

use a scaled down version of the image for display. 使用缩小版图像进行显示。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM