gpt4 book ai didi

iOS 如何调整图像和蒙版(图像或蒙版任何人都不能同时放大调整)

转载 作者:行者123 更新时间:2023-11-29 02:55:24 25 4
gpt4 key购买 nike

我正在开发一个应用程序,我将从相机或相机胶卷中拍摄照片并使用蒙版图像进行蒙版,但是我如何调整图像或蒙版?我尝试了 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];
}

最佳答案

在与 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;
}

关于iOS 如何调整图像和蒙版(图像或蒙版任何人都不能同时放大调整),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23957835/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com