1

I am making a camera app which includes digital zoom. I have a slider (zoomSlider) that has a minimum value of 1 and a maximum value of 4. When the user taps the camera button, it takes a picture, and then I crop it for the zooming. I have two problems:

How do I crop the exact middle of the image? (eg. 2x zoom, Rect would be centered with dimensions of 600x800 (for iPhone 2G/3G))

When I do this, it rotates the image. I make up for it by rotating the UIImageView it's in, but this causes a portrait picture to become landscape and vice versa.

Here is my code:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

     if ([mediaType isEqualToString:@"public.image"]){

UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

          CGRect clippedRect = CGRectMake(600, 450, image.size.width/zoomSlider.value, image.size.height/zoomSlider.value);

          UIImage *cropped = [self imageByCropping:image toRect:clippedRect];

          CGRect croppedImageSize = CGRectMake(0, 0, image.size.width/zoomSlider.value, image.size.height/zoomSlider.value);

          [cropped drawInRect:croppedImageSize];

          zoomPhoto.frame = croppedImageSize;

          zoomPhoto.image = cropped;

CGAffineTransform rotateTransform = CGAffineTransformRotate(CGAffineTransformIdentity,

                                                                                     RADIANS(90.0));

          zoomPhoto.transform = rotateTransform;
}

- (UIImage *)imageByCropping:(UIImage *)imageToCrop toRect:(CGRect)rect

{

     CGImageRef imageRef = CGImageCreateWithImageInRect([imageToCrop CGImage], rect);



     UIImage *cropped = [UIImage imageWithCGImage:imageRef];

CGImageRelease(imageRef);




 return cropped;

}

4

1 回答 1

1
CGFloat newWidth = image.size.width/zoomSlider.value;
CGFloat newHeight = image.size.height/zoomSlider.value;

CGRect clippedRect = CGRectMake((image.size.width-newWidth)/2, (image.size.height-newHeight)/2, newWidth, newHeight);

删除它以不旋转图像:

CGAffineTransform rotateTransform = CGAffineTransformRotate(CGAffineTransformIdentity,RADIANS(90.0));

zoomPhoto.transform = rotateTransform;
于 2010-01-21T04:25:27.197 回答