【问题标题】:crop a zoomed image in ImageView inside scrollView在 scrollView 内的 ImageView 中裁剪缩放图像
【发布时间】:2012-07-11 21:06:58
【问题描述】:

我已经在我身边做了很多努力。最后我需要帮助。谢谢

目标: 1) 我如何将 imageView 放入 ScrollView

2) 我如何在 scrollView 中裁剪放大的图像。

我在滚动视图中有一个 imageView。我想要缩放后的裁剪图像,它显示在滚动视图边界内。我已经裁剪了图像,但它与我想要的不完全相同。

在这里,我将 backgroundColor Black 设置为我的滚动视图。而且当我把imageView放在里面的时候,不合适。

在滚动视图内缩放图像后

裁剪后的图片是

我的代码在这里:

- (void)viewDidLoad
{
    [super viewDidLoad];

    CGRect frame1 = CGRectMake(50,50,200,200);

    CGRect frame2 = CGRectMake(50,50,200,200);

    imageView1=[[UIImageView alloc]initWithFrame:frame1];

   imageView1.image= [UIImage imageNamed:@"back.jpeg"];

   imageView1.backgroundColor=[UIColor brownColor];

   imageView1.contentMode = UIViewContentModeScaleAspectFit;



   scroll=[[UIScrollView alloc]initWithFrame:frame2];

   scroll.backgroundColor=[UIColor blackColor];

  [scroll addSubview:imageView1];

  scroll.delegate=self;

 [self.view addSubview:scroll];

 [self setContentSizeForScrollView];

 self.imageView1.userInteractionEnabled = YES;

}

设置滚动视图内容大小

-(void)setContentSizeForScrollView
{
 // scroll.contentSize = CGSizeMake(imageView1.frame.size.width,imageView1.frame.size.height);

    scroll.contentSize = CGSizeMake(200, 200);
   scroll.minimumZoomScale = .50;
  scroll.maximumZoomScale = 1.5;
}

我的裁剪逻辑是

-(IBAction)cropButtonClicked
{
    //Calculate the required area from the scrollview

CGRect rect = CGRectMake(50, 50, 200,200);


UIImage *image = [self imageByCropping:imageView1.image toRect:rect];

imageView1.image=image;


imageView1.contentMode = UIViewContentModeScaleAspectFit;


 }

这种方法裁剪图像:

- (UIImage*)imageByCropping:(UIImage *)myImage toRect:(CGRect)cropToArea{
    CGImageRef cropImageRef = CGImageCreateWithImageInRect(myImage.CGImage, cropToArea);
    UIImage* cropped = [UIImage imageWithCGImage:cropImageRef];

    CGImageRelease(cropImageRef);
    return cropped;
}

【问题讨论】:

    标签: iphone ios crop


    【解决方案1】:

    我自己的问题的答案:经过多次努力,我找到了两个问题的答案。

    它对我很有用。我在这里分享,可能对某人有帮助。 :)

    1) 使图像视图适合滚动视图。我用这个link

    - (void)centerScrollViewContents {
    
    CGSize boundsSize = scroll.bounds.size;
    CGRect contentsFrame = self.imageView1.frame;
    
    if (contentsFrame.size.width < boundsSize.width) {
    contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width) / 2.0f;
    } 
    else {
    contentsFrame.origin.x = 0.0f;
    }
    
    if (contentsFrame.size.height < boundsSize.height) {
        contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height) / 2.0f;
    } 
    else {
    contentsFrame.origin.y = 0.0f;
    }
    
    self.imageView1.frame = contentsFrame;
    }
    

    2) 在 scrollView 中裁剪放大的图像。我用这个link

        UIGraphicsBeginImageContext(CGSizeMake(200, 200));
    
        [scroll.layer renderInContext:UIGraphicsGetCurrentContext()];
    
        UIImage *fullScreenshot = UIGraphicsGetImageFromCurrentImageContext();
    
        UIGraphicsEndImageContext();
    
        imageView1.contentMode = UIViewContentModeScaleAspectFill;
    
        UIImageWriteToSavedPhotosAlbum(fullScreenshot, nil, nil, nil);
    
        return fullScreenshot;
    

    【讨论】:

      【解决方案2】:

      我最近也遇到了类似的问题,我用相对的思路( Xa,b = Xa,c - Xb,c )得到了解决方案

      -(IBAction)crop:(id)sender{
      
              // F = frame
              // i = image
              // iv = imageview
              // sv = self.view
              // of = offset
      
              //Frame Image in imageView coordinates
              CGRect Fi_iv = [self frameForImage:self.image inImageViewAspectFit:self.imageView];
      
              //Frame ImageView in self.view coordinates
              CGRect Fiv_sv = self.imageView.frame;
      
              //Frame Image in self.view coordinates
              CGRect Fi_sv = CGRectMake(Fi_iv.origin.x + Fiv_sv.origin.x
                                        ,Fi_iv.origin.y + Fiv_sv.origin.y,
                                        Fi_iv.size.width, Fi_iv.size.height);
              //ScrollView offset
              CGPoint offset = self.scrollView.contentOffset;
      
              //Frame Image in offset coordinates
              CGRect Fi_of = CGRectMake(Fi_sv.origin.x - offset.x,
                                              Fi_sv.origin.y - offset.y,
                                              Fi_sv.size.width,
                                              Fi_sv.size.height);
      
              CGFloat scale = self.imageView.image.size.width/Fi_of.size.width;
      
              //the crop frame in image offset coordinates
              CGRect Fcrop_iof = CGRectMake((self.cropView.frame.origin.x - Fi_of.origin.x)*scale,
                                            (self.cropView.frame.origin.y - Fi_of.origin.y)*scale,
                                            self.cropView.frame.size.width*scale,
                                            self.cropView.frame.size.height*scale);
      
              UIImage *image = [self image:self.imageView.image cropRect:Fcrop_iof];
              // Use this crop image...
      

      您可以使用以下方法裁剪图像:

      //Use this code to crop when you have the right frame
      -(UIImage*)image:(UIImage *)image cropRect:(CGRect)frame
      {
          // Create a new UIImage
          CGImageRef imageRef = CGImageCreateWithImageInRect(image.CGImage, frame);
          UIImage *croppedImage = [UIImage imageWithCGImage:imageRef];
          CGImageRelease(imageRef);
      
          return croppedImage;
      }
      

      由于在 Aspect Fit 中使用的是 ImageView,因此需要计算 imageView 内的图像帧

      -(CGRect)frameForImage:(UIImage*)image inImageViewAspectFit:(UIImageView*)imageView
      {
          float imageRatio = image.size.width / image.size.height;
      
          float viewRatio = imageView.frame.size.width / imageView.frame.size.height;
      
          if(imageRatio < viewRatio)
          {
              float scale = imageView.frame.size.height / image.size.height;
      
              float width = scale * image.size.width;
      
              float topLeftX = (imageView.frame.size.width - width) * 0.5;
      
              return CGRectMake(topLeftX, 0, width, imageView.frame.size.height);
          }
          else
          {
              float scale = imageView.frame.size.width / image.size.width;
      
              float height = scale * image.size.height;
      
              float topLeftY = (imageView.frame.size.height - height) * 0.5;
      
              return CGRectMake(0, topLeftY, imageView.frame.size.width, height);
          }
      }
      

      我希望这段代码对你有用,对我也有用。

      最好的,

      拉斐尔。

      【讨论】:

      • 我很喜欢这个。我在上面的代码中发现的唯一问题是在标题“//图像偏移坐标中的裁剪框”下面的代码中。您使用的是 self.cropview.frame 而我使用的是 self.cropview.bounds。
      猜你喜欢
      • 1970-01-01
      • 2016-03-29
      • 2016-08-29
      • 2017-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-18
      • 1970-01-01
      相关资源
      最近更新 更多