ios - 缩放用于在 IOS 中调整和裁剪图像的 uiscrollview 时出现问题

标签 ios objective-c uiscrollview uiimageview pinchzoom

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

下面是我的代码:

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

-(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];

但什么也没做..

最佳答案

问题解决了,如果有人想使用 scrollview 来裁剪和缩放,而 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);

}

关于ios - 缩放用于在 IOS 中调整和裁剪图像的 uiscrollview 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22795350/

相关文章:

android - C++识别运行平台(Android、iOS等)

ios - 为 iOS/Xcode 的 UITextView 手动格式化字符串中以 http 或 https 开头的 url

iphone - 如何使用 UIKit/Quartz 在 iPhone 上的纹理上绘制灯光效果

iOS 9、Swift 2.0、Xcode 和测试

ios - Objective-C : Create multiple UIPickerView with UITextFields programmatically

ios - 我如何使用 UIScrollView 来适应图像的缩放部分?

ios - UIImageView 快速缩放和拖动

ios - MKMapView 叠加矢量瓦片

ios - 让 UIScrollView 与 AutoLayout 一起工作

java - 适用于所有浏览器的数据可视化库