【问题标题】:Issue while zooming uiscrollview which is used to resize and crop Image in IOS缩放用于在 IOS 中调整大小和裁剪图像的 uiscrollview 时出现问题
【发布时间】:2014-05-12 18:51:33
【问题描述】:

我正在开发一个在其中一个视图中具有(图像保存)的应用程序,我希望在用户缩放和缩放从滚动视图捕获的图像后保存我的图像的三种不同尺寸当用户点击保存时。

以下是我的代码:

选择样式后,根据选择的类型设置宽度和高度,然后调用滚动视图创建方法

-(void)viewDidAppear:(BOOL)animated{
if(!([getColor length] == 0))
{
    [theColored setHidden:NO];
    [imageView setImage:image];
    [theStyle setHidden:NO];
    [theStyled setHidden:YES];

}
if(!([getStyle length] == 0))
{
    [theColored setHidden:NO];
    [imageView setImage:image];
    [theStyle setHidden:YES];
    [theStyled setHidden:NO];
    [Save setHidden:NO];


    if([theType  isEqual: @"Pant"]){
        theW = 200;
        theH = 260;
    }else if ([theType isEqual:@"Shirt"])
    {
        theW = 220;
        theH = 220;
    }else if([theType isEqual:@"Shoes"])
    {
        theW = 200;
        theH = 60;
    }
   // UIImage *savedImage = image;

    CGFloat theScale = 1.0;
    [self setTheView:image andFrameW:&theW andFrameH:&theH andTheScale:&theScale];

      }}

方法如下:

-(void)setTheView:(UIImage *)savedImage andFrameW:(CGFloat *)theFW andFrameH:(CGFloat *)theFH andTheScale:(CGFloat *)theScale{
    NSLog(@"called");
    CGFloat theS = *theScale;
    CGFloat imgW ;
    CGFloat imgH;
    imgW = *theFW * theS;
    imgH = *theFH * theS;




    // = savedImage.size.height * theS;
    [dot removeFromSuperview];
    [scrollView setPagingEnabled:NO];
    [scrollView setShowsHorizontalScrollIndicator:NO];
    [scrollView setShowsVerticalScrollIndicator:NO];
    scrollView.layer.masksToBounds=YES;
    scrollView.minimumZoomScale = 0.1f;
    scrollView.maximumZoomScale = 5.0f;
  [scrollView setZoomScale:0.5];
    scrollView.layer.borderColor = [UIColor blackColor].CGColor;
    scrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    [scrollView setContentSize:CGSizeMake(imgW*2,imgH*2)];

    dot =[[UIImageView alloc] initWithFrame:CGRectMake(imgW/2,imgH/2,imgW,imgH)];
    dot.image=savedImage;
    dot.contentMode = UIViewContentModeScaleAspectFit;


    [scrollView setScrollEnabled:YES];
    [scrollView setTranslatesAutoresizingMaskIntoConstraints:YES];
    scrollView.frame = CGRectMake(scrollView.frame.origin.x,scrollView.frame.origin.y, *theFW , *theFH);
    [scrollView setHidden:NO];
    [scrollView setContentOffset:CGPointMake(imgW/2,imgH/2) animated:NO];
    [scrollView addSubview:dot];


}

缩放方法:

- (UIView *) viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return dot;
}

- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale{
    NSLog(@"zoomed");
    NSLog(@"%f",scale);
   [self setTheView:image andFrameW:&theW andFrameH:&theH andTheScale:&scale];

}

问题: 我这里有两个问题:

1- 缩放比例不是从它到达的最后一点开始。

2- 在点击缩放时,甚至在开始缩放之前也会移动。

更新编辑

两个问题都解决了

缩放后我没有再次调用该方法,而是更改了内容大小

- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale{

[self setTheView:image andFrameW:&theW andFrameH:&theH andTheScale:&scale];

}

替换为

- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale{

scrollView.contentSize = CGSizeMake((dot.bounds.size.width*scale)*2,(dot.bounds.size.height*scale)*2);

}

但是我有新的问题出现了,缩放后contentOffSet不等于上下并且还左写

图解说明:

<----------------------------Content Size---------------------------->
|    left offset    |<---------Scrollviewframe--------->|Right offset|

我尝试设置新的内容偏移:

[scrollView setContentOffset:CGPointMake((dot.bounds.size.width*scale)/2,(dot.bounds.size.height*scale)/2) animated:NO];

但什么也没做..

【问题讨论】:

    标签: ios objective-c uiscrollview uiimageview pinchzoom


    【解决方案1】:

    问题已解决,如果有人想使用滚动视图进行裁剪和缩放,而 allowsEditing = YES; 无法设置矩形大小,我想分享答案

    - (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale{
    
    CGFloat zoomedW = dot.bounds.size.width*scale;
    CGFloat zoomedH = dot.bounds.size.height*scale;
    CGFloat frameW = scrollView.frame.size.width;
    CGFloat frameH = scrollView.frame.size.height;
    
    scrollView.contentSize = CGSizeMake(frameW+zoomedW ,frameH+zoomedH);
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多