iphone - 连续拍照时 iOS 应用程序崩溃

标签 iphone objective-c ios

在我的应用程序中,我通过单击按钮连续拍摄五张照片。但是,当我在拍摄这张照片的过程中按下设备上的主页按钮时,应用程序像往常一样进入后台阶段,但当我尝试重新启动时,应用程序崩溃了。您能否针对此问题提出任何解决方案?

提前致谢

最佳答案

您好,请引用这三种方法,这可能会解决您的内存泄漏问题,实际上崩溃是由于内存泄漏造成的,因为有时当我们拍摄更多高分辨率照片并且我们正在使用它是我们的应用程序时,iphone 无法处理太多更多内存。

还有一件事我需要说对不起,因为我不知道如何格式化代码。

  • (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { [选择器 dismissModalViewControllerAnimated:YES]; [选择器释放];

    [popover dismissPopoverAnimated:YES]; [弹出窗口释放];

    UIImage *capturedImage = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; [ self 执行选择器:@选择器(waitUntillImageCaptured:) withObject:capturedImage afterDelay:0.2];

    -(void)waitUntillImageCaptured:(UIImage *)originalImage {
    UIImage *tempimg;

    tempimg = [self scaleAndRotateImage:originalImage];

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    NSString *imagePath = [[appDelegate applicationDocumentDirectoryString] stringByAppendingPathComponent:@"ClientTempthumb.png"];

    //UIImageView *tempView = [[UIImageView alloc] initWithImage:tempimg]; NSData *data = [NSData dataWithData:UIImagePNGRepresentation(tempimg)];

    如果(数据!=无) { [数据 writeToFile:imagePath 原子地:YES]; } 别的 { [[NSFileManager defaultManager] removeItemAtPath:imagePath 错误:NULL];

    [btnPhoto setImage:tempimg forState:UIControlStateNormal]; [池释放]; //[appDelegate hideLoadingView];

    • (UIImage *)scaleAndRotateImage:(UIImage *)image { int kMaxResolution = 550;//或者随便什么

    CGImageRef imgRef = image.CGImage;

    CGFloat 宽度 = CGImageGetWidth(imgRef); CGFloat height = CGImageGetHeight(imgRef);

    CGAffineTransform 变换 = CGAffineTransformIdentity; CGRect 边界 = CGRectMake(0, 0, 宽度, 高度); 如果(宽度 > kMaxResolution || 高度 > kMaxResolution){ CGFloat ratio = width/height;

    if (ratio > 1) 
    {
        bounds.size.width = kMaxResolution;
        bounds.size.height = roundf(bounds.size.width / ratio);
    }
    else 
    {
        bounds.size.height = kMaxResolution;
        bounds.size.width = roundf(bounds.size.height * ratio);
    }
    

    CGFloat scaleRatio = bounds.size.width/width; CGSize imageSize = CGSizeMake(CGImageGetWidth(imgRef), CGImageGetHeight(imgRef)); CGFloat 绑定(bind)高度; UIImageOrientation orient = image.imageOrientation;

    切换(方向) {

    case UIImageOrientationUp: //EXIF = 1
    
        // landscape right
        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
    
        // landscape left
        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
    
        // Portrait Mode 
        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"];
    

    UIGraphicsBeginImageContext(bounds.size);

    CGContextRef context = UIGraphicsGetCurrentContext();

    if (orient == UIImageOrientationRight || orient == UIImageOrientationLeft) { CGContextScaleCTM(上下文,-scaleRatio,scaleRatio); CGContextTranslateCTM(context, -height, 0); } 别的 { CGContextScaleCTM(上下文,scaleRatio,-scaleRatio); CGContextTranslateCTM(context, 0, -height);

    CGContextConcatCTM(context, transform);

    CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, 宽度, 高度), imgRef); UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext();

    返回图片副本;

关于iphone - 连续拍照时 iOS 应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12564782/

相关文章:

iphone - 应用 CGAffineTransform 旋转后计算偏移量

ios - NSDateFormatter dateFromString在iOS 13问题中返回nil

ios - UIImagePickerController 显示的 VIDEO_TOO_LONG_TITLE 警报

ios - 图片不在项目文件夹中

iPhone YouTube channel 应用程序

iPhone 6.1 模拟器

objective-c - 我应该如何构造一个具有多个键的 NSDictionary?

objective-c - 手指画的效率问题

ios - 在应用内购买期间观察相同警报 View 的问题

ios - 无法为实体 'MyLocations.Location' 加载名为 'Location' 的类