图像旋转和缩放

时间:2013-09-11 08:59:49

标签: iphone ios objective-c

我需要在UIImageView的帮助下旋转和缩放UISlider图片。

UISlider值处于中间位置时有三种情况,原始图像将是原始位置。
第二种情况,当滑块值最大时,图像将以45度的比例旋转旋转 第三种情况,当滑块值处于其最小位置时,图像将以相反方向旋转45度,并且还会比特尺度旋转 我使用了这段代码,但没有得到理想的结果。

-(IBAction)sliderMoved:(id)sender
{
    UIImage *image = [UIImage imageNamed:@"landscape.jpg"]; 
    photoImage.transform = CGAffineTransformMakeRotation(slider.value * 2*M_PI_2 / slider.maximumValue);
    CGFloat scale=0.5;
    NSLog(@"sliderVlaue=%f",slider.value);
    if ((slider.value) >(0.75)) {
        scale =(.25+ slider.value);
    } else {
        scale =(0.75 +slider.value);
    }
    CGAffineTransform currentTransform = photoImage.transform;
    CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);
    [photoImage setTransform:newTransform];
}

4 个答案:

答案 0 :(得分:0)

你可以试试这段代码

CGFloat Angle=(((int)slider.value*M_PI)/180);
transform=CGAffineTransformScale(transform, photoImage.scale, photoImage.scale);
transform= CGAffineTransformRotate(transform,Angle);
photoImage.transform = transform;

答案 1 :(得分:0)

您可以使用此代码进行图像旋转

imageview.transform = CGAffineTransformMakeRotation(M_PI);

用于缩放图像

-(IBAction)grow
{   
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.5];
    [UIView setAnimationRepeatCount:1];
    [UIView setAnimationRepeatAutoreverses:YES];
    CGRect b = grower.bounds;
    b.size.height = 200;
    b.size.width = 200;
    grower.bounds =  b;
    [UIView commitAnimations];
}

答案 2 :(得分:0)

您可以使用以下代码执行此操作:

- (void)viewDidLoad
{
    [super viewDidLoad];
    imgToRotate = [[UIImageView alloc]initWithFrame:CGRectMake(48, 208, 240, 240)];
    imgToRotate.image = [UIImage imageNamed:@"rtm.jpeg"];
    [self.view addSubview:imgToRotate];  
}


- (IBAction)slideToRotateScale:(UISlider *)sender
{
    sender.maximumValue = 1.0;
    sender.minimumValue = -1.0;
    imgToRotate.transform = CGAffineTransformRotate(CGAffineTransformMakeScale(fabsf(sender.value), fabs(sender.value)), sender.value*M_PI/4);
}

答案 3 :(得分:0)

  

我在给定提示的帮助下尝试了自己,并能够解决问题。这是代码`

in you h file declare  UIImageView *photoImage;
UIView *canvas;

CAShapeLayer *_marque;

在视图中加载了

if (!_marque) {
    _marque = [CAShapeLayer layer] ;
    _marque.fillColor = [[UIColor clearColor] CGColor];
    _marque.strokeColor = [[UIColor grayColor] CGColor];
    _marque.lineWidth = 1.0f;
    _marque.lineJoin = kCALineJoinRound;
    _marque.lineDashPattern = [NSArray arrayWithObjects:[NSNumber numberWithInt:10],[NSNumber numberWithInt:5], nil];
    _marque.bounds = CGRectMake(photoImage.frame.origin.x, photoImage.frame.origin.y, 0, 0);
    _marque.position = CGPointMake(photoImage.frame.origin.x + canvas.frame.origin.x, photoImage.frame.origin.y + canvas.frame.origin.y);
}
[[self.view layer] addSublayer:_marque];

    - (IBAction) sliderMoved:(id)sender
     {

    CGFloat scale=0;
   NSLog(@"sliderVlaue=%f",slider.value);
   if ((slider.value) >(0.77)) {
    scale =(.5+ slider.value);
    photoImage.transform = CGAffineTransformMakeRotation(((4*slider.value)*(M_PI/180)));
    CGAffineTransform currentTransform = photoImage.transform;
    CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale-.25, scale-.25);

    [photoImage setTransform:newTransform];

    NSLog(@"max=%f",scale);


}else if(slider.value< 0.77 && slider.value >.73){
   photoImage.transform=CGAffineTransformIdentity;
    photoImage.frame=imageframe;
     scale =(0.5 +slider.value);
    NSLog(@"mid=%f",scale);
}
else{
    scale =(.77 +(1.0-slider.value));
    photoImage.transform = CGAffineTransformMakeRotation((((-4)*(slider.value))*(M_PI/180)));
    CGAffineTransform currentTransform = photoImage.transform;
    CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);

    [photoImage setTransform:newTransform];
     NSLog(@"min=%f",scale);

}


    //_lastScale = 1;
[self showOverlayWithFrame:photoImage.frame];
}

-(void)showOverlayWithFrame:(CGRect)frame {

if (![_marque actionForKey:@"linePhase"]) {
    CABasicAnimation *dashAnimation;
    dashAnimation = [CABasicAnimation animationWithKeyPath:@"lineDashPhase"];
    [dashAnimation setFromValue:[NSNumber numberWithFloat:0.0f]];
    [dashAnimation setToValue:[NSNumber numberWithFloat:15.0f]];
    [dashAnimation setDuration:0.5f];
    [dashAnimation setRepeatCount:HUGE_VALF];
    [_marque addAnimation:dashAnimation forKey:@"linePhase"];
}

_marque.bounds = CGRectMake(frame.origin.x, frame.origin.y, 0, 0);
_marque.position = CGPointMake(frame.origin.x + canvas.frame.origin.x, frame.origin.y + canvas.frame.origin.y);

CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRect(path, NULL, frame);
[_marque setPath:path];
CGPathRelease(path);
 _marque.hidden = NO;    
  }