-1

In my app I am taking images from the camera and saving them in an imageView. My code is working fine on a iPad, iPhone 5/6 but not on iPhone 4s iOS 9. Also, it is giving a memory warning in iPhone 4s.

When I debug image changed in imageview after dismissing of imagePicker. Default image of profile view remains there, but the debugger shows that the image is changed.

Initialize picker like this:

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];

        }];
    }
}

When user takes image and delegate called I use this code:

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

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

    [picker dismissViewControllerAnimated:YES completion:nil];
}

I also tried to save picker image in a variable to save memory like this:

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

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

    [picker dismissViewControllerAnimated:YES completion:nil];
}

Also I use dispatch_async main but in iPhone 4s it is not working.

Peppo
  • 1,107
  • 1
  • 12
  • 19
Divyanshu Sharma
  • 551
  • 2
  • 12

2 Answers2

0

You should initialize your Camera in your UIViewController, only after the view has Loaded and with a timeout:I get answer from 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];
}

//resize image

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

        [picker dismissViewControllerAnimated:YES completion:nil];
    } 

Please use for resize image size i had same issue i resolve it.

-(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;
        }
Community
  • 1
  • 1
Mitul Marsoniya
  • 5,272
  • 4
  • 33
  • 57
0

From what you describe, I don't believe your issue is code related. It sounds like it's a constraints issue where your UIImageView is collapsing out of shape so you the image isn't displayed on the smaller iPhone 4S screen.

In order to test that this the case, if you could make the UIImageView the size of your screen and pin each of the object's edges to corresponding edge of the screen. See pic, 4 constraints pinned to outside edges.

enter image description here

If the image is now displayed after testing with this, then you will know it's constraints related and not code so you will be able to adjust your Interface Builder constraints for this view accordingly.

I hope this helps.

Jim Tierney
  • 4,078
  • 3
  • 27
  • 48