作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发一个应用程序,我将从相机或相机胶卷中拍摄照片并使用蒙版图像进行蒙版,但是我如何调整图像或蒙版?我尝试了 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/
我是一名优秀的程序员,十分优秀!