HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – 根据UIPinch缩放图像大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我很困惑如何做我需要的!任何帮助非常感谢!我有一个透明的图像叠加层,后面有一个UIPanGestureRecognizer和一个UIPinchGestureRecognizer附在其上的另一个图像.顶部的图像有一个完全透明的中间,作为“玻璃”视图.我正在努力将底部的图像从平底锅和夹子中切割成“玻璃”部分. (见下图,看看我在说什么)我已经成功地照顾了平底锅作物,但是当应用捏合时,我正在对其进行正确裁剪.

我没有使用snapshotViewAfterScreenupdates.

以下是我到目前为止的代码

UIImage *snapshotImage;

/* draw the image of all the views below our view */
UIGraphicsBeginImageContextWithOptions(self.pendantImageView.bounds.size,NO,0);
BOOL successfulDrawHierarchy = [self.pendantImageView drawViewHierarchyInRect:self.pendantImageView.bounds afterScreenupdates:YES];
if ( successfulDrawHierarchy ) {
    snapshotImage = UIGraphicsGetImageFromCurrentImageContext();
} else {
    NSLog(@"drawViewHierarchyInRect:afterScreenupdates: Failed - there's nothing to draw...");
}
UIGraphicsEndImageContext();
UIImage *croppedImage;
if ( successfulDrawHierarchy ) {

    /* calculate the coordinates of the rectangle we'rE interested in within the returned image */
    CGRect cropRect = CGRectOffset(pendantFrame,- self.pendantImageView.frame.origin.x,- self.pendantImageView.frame.origin.y);

    /* draw the cropped section with a clipping region */
    UIGraphicsBeginImageContextWithOptions(cropRect.size,YES,0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextClipToRect(context,CGRectMake(0,cropRect.size.width,cropRect.size.height));
    CGRect targetRectangeForCrop = CGRectMake(-cropRect.origin.x,-cropRect.origin.y,snapshotImage.size.width,snapshotImage.size.height);
    [snapshotImage drawInRect:targetRectangeForCrop];
    croppedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();  
}

pendantImageView是底部的imageView,pendantFrame是我正在尝试裁剪的区域的中间坐标.

提前致谢!

解决方法

我不知道我是否正确理解你,但这是我的结果:

(点击视频观看完整版)

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    UIImageView *theImageView = [UIImageView new];
    [self.view addSubview:theImageView];
    theImageView.image = [UIImage imagenamed:@"image1.jpg"];
    theImageView.frame = self.view.frame;
    theImageView.userInteractionEnabled = YES;
    theImageView.alpha = 0.6;

    UIImageView *thePanImageView = [UIImageView new];
    [self.view addSubview:thePanImageView];
    thePanImageView.frame = CGRectMake(0,100,100);
    thePanImageView.center = CGPointMake(theImageView.frame.size.width/2,theImageView.frame.size.height/2);
    thePanImageView.image = [self screenshotFromRect:thePanImageView.frame fromView:theImageView];
    thePanImageView.userInteractionEnabled = YES;
    thePanImageView.layer.cornerRadius = thePanImageView.frame.size.width/2;
    thePanImageView.clipsToBounds = YES;
    thePanImageView.theWeakObject = theImageView;
    {
        UIPanGestureRecognizer *thePanGesture = [UIPanGestureRecognizer new];
        [thePanGesture addTarget:self action:@SELEctor(handlePanGesture:)];
        [thePanImageView addGestureRecognizer:thePanGesture];

        UIPinchGestureRecognizer *thePinchGesture = [UIPinchGestureRecognizer new];
        [thePinchGesture addTarget:self action:@SELEctor(handlePinchGesture:)];
        [thePanImageView addGestureRecognizer:thePinchGesture];
    }
}

- (void)handlePanGesture:(UIPanGestureRecognizer *)thePanGesture
{
    UIImageView *thePanImageView = (id)thePanGesture.view;

    UIImageView *theImageView = thePanImageView.theWeakObject;
    thePanImageView.center = [thePanGesture LOCATIOnInView:theImageView];

    thePanImageView.image = [self screenshotFromRect:thePanImageView.frame fromView:theImageView];
}

- (void)handlePinchGesture:(UIPinchGestureRecognizer *)thePinchGesture
{
    UIImageView *thePanImageView = (id)thePinchGesture.view;
    static CGRect theInitialFrame;
    if (thePinchGesture.state == UIGestureRecognizerStateBegan)
    {
        theInitialFrame = thePanImageView.frame;
    }
    else
    {
        CGRect theFrame = theInitialFrame;
        theFrame.size.width *= thePinchGesture.scale;
        theFrame.size.height *= thePinchGesture.scale;
        thePanImageView.frame = theFrame;
    }

    thePanImageView.layer.cornerRadius = thePanImageView.frame.size.width/2;

    UIImageView *theImageView = thePanImageView.theWeakObject;
    thePanImageView.center = [thePinchGesture LOCATIOnInView:theImageView];

    thePanImageView.image = [self screenshotFromRect:thePanImageView.frame fromView:theImageView];
}

- (UIImage * __nonnull)screenshotFromRect:(CGRect)theRect fromView:(UIView * __nonnull)theView;
{
    if (!theView)
    {
        abort();
    }
    if (theRect.size.height < 1 || theRect.size.width < 1)
    {
        abort();
    }

    if ([[UIScreen mainScreen] respondsToSELEctor:@SELEctor(scalE)])
    {
        UIGraphicsBeginImageContextWithOptions(theRect.size,[UIScreen mainScreen].scalE);
    }
    else
    {
        UIGraphicsBeginImageContext(theRect.sizE);
    }

    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextTranslateCTM(ctx,-theRect.origin.x,-theRect.origin.y);
    [theView.layer renderInContext:ctx];

    UIImage *snapshotImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return snapshotImage;
}

编辑:

上述解决方案在cpu方面效率低下,因为它每次移动视图时都需要截图.

一个更有效的方式是创建一个额外的UIImageView,并将其移动到您的PangImageView内

代码如下:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    UIImageView *theImageView = [UIImageView new];
    [self.view addSubview:theImageView];
    theImageView.image = [UIImage imagenamed:@"image1.jpg"];
    theImageView.frame = self.view.frame;
    theImageView.userInteractionEnabled = YES;
    theImageView.alpha = 0.6;

    UIImageView *thePanImageView = [UIImageView new];
    [self.view addSubview:thePanImageView];
    thePanImageView.frame = CGRectMake(0,theImageView.frame.size.height/2);
    thePanImageView.userInteractionEnabled = YES;
    thePanImageView.layer.cornerRadius = thePanImageView.frame.size.width/2;
    thePanImageView.clipsToBounds = YES;
    thePanImageView.layer.borderColor = [UIColor redColor].CGColor;
    thePanImageView.layer.borderWidth = 1;
    thePanImageView.theWeakObject = theImageView;
    {
        UIPanGestureRecognizer *thePanGesture = [UIPanGestureRecognizer new];
        [thePanGesture addTarget:self action:@SELEctor(handlePanGesture:)];
        [thePanImageView addGestureRecognizer:thePanGesture];

        UIPinchGestureRecognizer *thePinchGesture = [UIPinchGestureRecognizer new];
        [thePinchGesture addTarget:self action:@SELEctor(handlePinchGesture:)];
        [thePanImageView addGestureRecognizer:thePinchGesture];

        UIImageView *theExTraiR_245_11845@ageView = [UIImageView new];
        [thePanImageView addSubview:theExTraiR_245_11845@ageView];
        theExTraiR_245_11845@ageView.frame = CGRectMake(-thePanImageView.frame.origin.x,-thePanImageView.frame.origin.y,theImageView.frame.size.width,theImageView.frame.size.height);
        theExTraiR_245_11845@ageView.image = theImageView.image;
    }
}

- (void)handlePanGesture:(UIPanGestureRecognizer *)thePanGesture
{
    UIImageView *thePanImageView = (id)thePanGesture.view;

    UIImageView *theImageView = thePanImageView.theWeakObject;
    thePanImageView.center = [thePanGesture LOCATIOnInView:theImageView];

    UIImageView *theExTraiR_245_11845@ageView = thePanImageView.subviews.firstObject;
    theExTraiR_245_11845@ageView.frame = CGRectMake(-thePanImageView.frame.origin.x,theExTraiR_245_11845@ageView.frame.size.width,theExTraiR_245_11845@ageView.frame.size.height);
}

- (void)handlePinchGesture:(UIPinchGestureRecognizer *)thePinchGesture
{
    UIImageView *thePanImageView = (id)thePinchGesture.view;
    static CGRect theInitialFrame;
    if (thePinchGesture.state == UIGestureRecognizerStateBegan)
    {
        theInitialFrame = thePanImageView.frame;
    }
    else
    {
        CGRect theFrame = theInitialFrame;
        theFrame.size.width *= thePinchGesture.scale;
        theFrame.size.height *= thePinchGesture.scale;
        thePanImageView.frame = theFrame;
    }

    thePanImageView.layer.cornerRadius = thePanImageView.frame.size.width/2;

    UIImageView *theImageView = thePanImageView.theWeakObject;
    thePanImageView.center = [thePinchGesture LOCATIOnInView:theImageView];

    UIImageView *theExTraiR_245_11845@ageView = thePanImageView.subviews.firstObject;
    theExTraiR_245_11845@ageView.frame = CGRectMake(-thePanImageView.frame.origin.x,theExTraiR_245_11845@ageView.frame.size.height);
}

供参

weakObject只是我的NSObject的自定义属性.我使用它,因为我很懒惰,不想创建全局可见的@property

大佬总结

以上是大佬教程为你收集整理的ios – 根据UIPinch缩放图像全部内容,希望文章能够帮你解决ios – 根据UIPinch缩放图像所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。