iOS如何调整图像和蒙版(无论是图像还是蒙版,任何人都可以放大以同时调整两者)

时间:2014-05-30 15:11:52

标签: ios objective-c uiimageview mask

我正在做一个应用程序,我将从相机或相机胶卷拍摄照片,并使用蒙版图像进行遮罩,但我如何调整图像或蒙版?我尝试使用scrollview进行放大和缩小以及捏合手势吗?

- (void)viewDidLoad
{
    [super viewDidLoad];

    // for zoomin
    [self.imageview setImage:imageView.image];
    [self.scrollView setMaximumZoomScale:5.0f];
    [self.scrollView setClipsToBounds:YES];


    // for drag
    UIPinchGestureRecognizer *pinch=[[UIPinchGestureRecognizer alloc]initWithTarget:self     action:@selector(pinched:)];
    [self.imageview addGestureRecognizer:pinch];

}


- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return self.imageview;
}

- (void)pinched:(UIPinchGestureRecognizer *)sender {
    if (sender.scale >1.0f && sender.scale < 2.5f) {
        CGAffineTransform transform = CGAffineTransformMakeScale(sender.scale, sender.scale);
        imageView.transform = transform;
     }
 }

-(IBAction)camera:(id)sender {
    picker = [[UIImagePickerController alloc]init];
    picker.delegate = self;

    [picker setSourceType:(UIImagePickerControllerSourceTypeCamera)];
    [self presentViewController:picker1 animated:YES completion:NULL];
}

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

    img = [info valueForKey:UIImagePickerControllerOriginalImage];
    [self.imageview setImage:img];
    [self dismissViewControllerAnimated:YES completion:NULL];
}

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [picker dismissViewControllerAnimated:YES completion:NULL];
}
  - (UIImage*) maskImage:(UIImage *) image withMask:(UIImage *) mask
{
CGImageRef imageReference = image.CGImage;
CGImageRef maskReference = mask.CGImage;

CGImageRef imageMask = CGImageMaskCreate(CGImageGetWidth(maskReference),
                                         CGImageGetHeight(maskReference),
                                         CGImageGetBitsPerComponent(maskReference),
                                         CGImageGetBitsPerPixel(maskReference),
                                         CGImageGetBytesPerRow(maskReference),
                                         CGImageGetDataProvider(maskReference),
                                         NULL, // Decode is null
                                         YES // Should interpolate
                                         );

CGImageRef maskedReference = CGImageCreateWithMask(imageReference, imageMask);
CGImageRelease(imageMask);

UIImage *maskedImage = [UIImage imageWithCGImage:maskedReference];
CGImageRelease(maskedReference);

return maskedImage;

}

- (IBAction)mask:(id)sender {

    self.imageview.image = [self maskImage:img withMask:[UIImage imageNamed:@"18.jpg"]];
    [sender addTarget:self
               action:@selector(maskButtonClicked:)
     forControlEvents:UIControlEventTouchUpInside];
}

1 个答案:

答案 0 :(得分:1)

转换为CGImageRef时会丢失方向数据。

而不是imageWithCGImage:,请使用:

[UIImage imageWithCGImage:maskedReference scale:image.scale orientation:image.imageOrientation];

修改

蒙版也忽略了图像方向,因此最简单的方法可能只是在对图像方向进行任何操作之前对图像方向进行标准化。 See this answer for more details

获得图像后但在开始掩盖图像之前,请使用以下方法修复其方向:

- (UIImage *)normalizedImage:(UIImage *)inputImage 
{
    if (inputImage.imageOrientation == UIImageOrientationUp) return inputImage; 

    UIGraphicsBeginImageContextWithOptions(inputImage.size, NO, inputImage.scale);
    [inputImage drawInRect:(CGRect){0, 0, inputImage.size}];
    UIImage *normalizedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return normalizedImage;
}