5

I'm attempting to implement an iOS camera view that takes pictures that are square in shape (similar to Instagram). My code appears below. The first part, where the frame height is set to be equal to the frame width, is working as expected and the user is given a view that is square. The problem occurs later when I attempt to apply the frame (which is a CGRect property) to the image data using CGImageCreateWithImageInRect. I pass the frame rect to this method with the image. But the results are not cropped to be square. Instead the image retains the original default dimensions from the iOS camera. Can someone please tell me what I've done wrong? My understanding from the Apple documentation is that CGImageCreateWithImageInRect should select an image area of shape Rect from some starting x/y coordinate. But that doesn't seem to be happening.

//Set the frame size to be square shaped
UIView *view = imagePicker.view;
        frame = view.frame;
        frame.size.height = frame.size.width;
        view.frame = frame;

//Crop the image to the frame dimensions using CGImageCreateWithImageInRect
-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [self.popoverController dismissPopoverAnimated:true];

NSString *mediaType = [info
                       objectForKey:UIImagePickerControllerMediaType];
[self dismissModalViewControllerAnimated:YES];
if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
    UIImage *image = [info
                      objectForKey:UIImagePickerControllerOriginalImage];

    croppedImage = (__bridge UIImage *)(CGImageCreateWithImageInRect((__bridge CGImageRef)(image), frame));

    imageView.image = croppedImage;

}
else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie])
{
    // Code here to support video if enabled
}
}
hughesdan
  • 3,019
  • 12
  • 57
  • 80

2 Answers2

0

You are doing right. The only thing is that I think you are setting the frame property same as the picker view, so the final size is the same as the original size.

Try to set frame smaller than pickerView.view.frame, not equal

Check this out

Cropping an UIImage

Community
  • 1
  • 1
user1447414
  • 1,306
  • 2
  • 12
  • 25
  • I tried passing an arbitrarily small CGRect but the output has 0 bytes of image data. I used CGRect rect = CGRectMake(0, 26, 50, 50);. Also, my original code sets the frame height to be equal to the width, so that's smaller than the original size. It's essentially a 320x320 square based on the dimensions of the iPhone screen. – hughesdan Oct 07 '12 at 16:15
0

You are setting the frame wrong. I suggest you take a look at this sample code from Apple, on how to create what you are trying to:

https://developer.apple.com/library/mac/#samplecode/VirtualScanner/Listings/Sources_VirtualScanner_m.html#//apple_ref/doc/uid/DTS40011006-Sources_VirtualScanner_m-DontLinkElementID_9

Look at the:

- (ICAError)startScanningWithParams:(ICD_ScannerStartPB*)pb

function

Lefteris
  • 14,550
  • 2
  • 56
  • 95