0

在我的应用程序中,单击按钮即可按顺序拍摄五张照片。但是,当我在这张照片拍摄过程中按下设备中的主页按钮时,应用程序像往常一样进入其后台阶段,但是当我尝试重新启动时,应用程序崩溃了。你能建议任何解决这个问题的方法吗?

提前致谢

4

1 回答 1

2

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

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

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

    [popoverdismissPopoverAnimated:YES]; [弹出框发布];

    UIImage *capturedImage = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; [self performSelector:@selector(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)];

    if(data != nil) { [data writeToFile:imagePath atomically:YES]; } else { [[NSFileManager defaultManager] removeItemAtPath:imagePath error:NULL]; }

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

    • (UIImage *)scaleAndRotateImage:(UIImage *)image { int kMaxResolution = 550; // 管他呢

    CGImageRef imgRef = image.CGImage;

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

    CGAffineTransform 变换 = CGAffineTransformIdentity; CGRect bounds = CGRectMake(0, 0, width, height); if (width > kMaxResolution || height > 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 boundHeight; UIImageOrientation 方向 = 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 上下文 = UIGraphicsGetCurrentContext();

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

    CGContextConcatCTM(上下文,变换);

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

    返回图像复制;}

于 2012-09-24T13:18:06.833 回答