【问题标题】:ImagePicker Issue iOS 9ImagePicker 问题 iOS 9
【发布时间】:2016-04-02 08:45:30
【问题描述】:

在我的应用程序中,我从相机拍摄图像并将它们保存在 imageView 中。我的代码在 iPad、iPhone 5/6 上运行良好,但在 iPhone 4s iOS 9 上运行良好。此外,它在 iPhone 4s 上发出内存警告。

当我在关闭 imagePicker 后调试 imageview 中更改的图像时。配置文件视图的默认图像仍然存在,但调试器显示图像已更改。

像这样初始化选择器:

if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
    UIImagePickerController *imagePicker = [UIImagePickerController new];

    imagePicker.delegate = self;

    imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;

    if( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){

        UIPopoverController* popOverController = [[UIPopoverController alloc] initWithContentViewController:imagePicker];

        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            [popOverController presentPopoverFromRect:_imgProfile.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
        }];


    }else {
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            [self presentViewController:imagePicker animated:YES completion:nil];

        }];
    }
}

当用户拍摄图像并调用委托时,我使用以下代码:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [[NSOperationQueue mainQueue]addOperationWithBlock:^{

        _imgProfile.image = [global scaleAndRotateImage:[info objectForKey:UIImagePickerControllerOriginalImage]];
    }];

    [picker dismissViewControllerAnimated:YES completion:nil];
}

我还尝试将选择器图像保存在变量中以节省内存,如下所示:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [[NSOperationQueue mainQueue]addOperationWithBlock:^{

        UIImage *img = [info objectForKey:UIImagePickerControllerOriginalImage];
        _imgProfile.image = img;
    }];

    [picker dismissViewControllerAnimated:YES completion:nil];
}

我也使用 dispatch_async main 但在 iPhone 4s 中它不起作用。

【问题讨论】:

  • 您是否有理由到处使用 NSOperationQueue?我的意思是你不能没有它吗?我不认为将它添加到本地 img 变量会有任何不同。
  • 我在堆栈溢出的某个地方发现使用 NSOperation 解决了这个问题......但这不是这个问题的解决方案
  • 你能不使用 NSOperationQueue 试试这个吗?然后会发生什么?
  • NSOperation 保证该方法何时被调用。
  • 即使在删除 NSOperationQueue 后这也不起作用

标签: ios objective-c iphone ios9


【解决方案1】:

根据您的描述,我认为您的问题与代码无关。听起来这是一个约束问题,您的UIImageView 折叠变形了,因此您的图像不会显示在较小的 iPhone 4S 屏幕上。

为了测试这种情况,如果您可以将 UIImageView 设置为屏幕大小并将对象的 每个 边缘固定到屏幕的相应边缘。参见图片,4 个约束固定到外边缘。

如果图像在测试后现在显示,那么您将知道它与constraints 相关且不是代码,因此您将能够调整您的界面生成器相应地限制此视图。

我希望这会有所帮助。

【讨论】:

    【解决方案2】:

    你应该在你的 UIViewController 中初始化你的相机,只有在视图加载并且超时之后:我从here得到答案

    - (void)viewDidAppear:(BOOL)animated 
    {
        [super viewDidAppear:animated];
        //show camera...
        if (!hasLoadedCamera)
            [self performSelector:@selector(showcamera) withObject:nil afterDelay:0.3];
    }
    
    - (void)showcamera {
        imagePicker = [[UIImagePickerController alloc] init];
        [imagePicker setDelegate:self];
        [imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
        [imagePicker setAllowsEditing:YES];
    
        [self presentModalViewController:imagePicker animated:YES];
    }
    

    //调整图片大小

        - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
        {
                UIImage *img = [self ResizesetImageOriantation:[info objectForKey:UIImagePickerControllerOriginalImage]];
                _imgProfile.image = img;
    
            [picker dismissViewControllerAnimated:YES completion:nil];
        } 
    

    请用于调整图像大小,我遇到了同样的问题,我解决了。

    -(UIImage *)ResizesetImageOriantation:(UIImage *)img
            {
                int kMaxResolution = 700; // Or whatever
    
                CGImageRef imgRef = img.CGImage;
    
                CGFloat width = CGImageGetWidth(imgRef);
                CGFloat height = CGImageGetHeight(imgRef);
    
                CGAffineTransform transform = 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 orient = img.imageOrientation;
                switch(orient) {
    
                    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(context, -scaleRatio, scaleRatio);
                    CGContextTranslateCTM(context, -height, 0);
                }
                else {
                    CGContextScaleCTM(context, scaleRatio, -scaleRatio);
                    CGContextTranslateCTM(context, 0, -height);
                }
    
                CGContextConcatCTM(context, transform);
    
                CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, width, height), imgRef);
                UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext();
                UIGraphicsEndImageContext();
    
                return imageCopy;
            }
    

    【讨论】:

    • 对尚未渲染的视图进行快照会导致快照为空。确保在快照或屏幕更新后的快照之前至少渲染一次视图。和接收内存警告
    • 欢迎 :) @DivyanshuSharma
    猜你喜欢
    • 1970-01-01
    • 2015-12-19
    • 2015-12-12
    • 2015-12-14
    • 2015-12-28
    • 2015-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多