ImagePicker问题iOS 9

时间:2015-12-28 13:37:06

标签: ios objective-c iphone ios9

在我的应用程序中,我从相机拍摄图像并将其保存在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中它无效。

2 个答案:

答案 0 :(得分:0)

您应该在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;
        }

答案 1 :(得分:0)

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

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

enter image description here

如果图片现在在测试后显示,那么您将知道它是constraints相关和代码,因此您可以调整相应地,此视图的Interface Builder约束。

我希望这会有所帮助。