2017-11-10 7 views
0

UIImageViewの画像を何回か回転させるボタンを作成したいと思います。その後、私はそれを外部のAPIに送る必要があります。私はプロポーションを失うObjective-C - 画像の割合を失うことなく、UIImageView内の画像を回転する

(UIImage *)imageRotatedByDegrees:(UIImage*)oldImage deg:(CGFloat)degrees 
{ 
    // calculate the size of the rotated view's containing box for our drawing space 
    UIView *rotatedViewBox = [[UIView alloc]  initWithFrame:CGRectMake(0,0,oldImage.size.width, oldImage.size.height)]; 
    CGAffineTransform t = CGAffineTransformMakeRotation(degrees * M_PI/180); 
    rotatedViewBox.transform = t; 
    CGSize rotatedSize = rotatedViewBox.frame.size; 
    // Create the bitmap context 
    UIGraphicsBeginImageContext(rotatedSize); 
    CGContextRef bitmap = UIGraphicsGetCurrentContext(); 

    // Move the origin to the middle of the image so we will rotate and scale around the center. 
    CGContextTranslateCTM(bitmap, rotatedSize.width/2, rotatedSize.height/2); 

    // // Rotate the image context 
    CGContextRotateCTM(bitmap, (degrees * M_PI/180)); 

    // Now, draw the rotated/scaled image into the context 
    CGContextScaleCTM(bitmap, 1.0, -1.0); 
    CGContextDrawImage(bitmap, CGRectMake(-oldImage.size.width/2, -oldImage.size.height/2, oldImage.size.width, oldImage.size.height), [oldImage CGImage]); 

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

オプション2:

私は二つの選択肢

オプション1を試してきた(画像のように)の割合

Image after rotatation Image before rotation

を失う必要はありません

(UIImage*)upsideDownBunny:(CGFloat)radians withImage:(UIImage*)testImage { 

    __block CGImageRef cgImg; 
    __block CGSize imgSize; 
    __block UIImageOrientation orientation; 
    dispatch_block_t createStartImgBlock = ^(void) { 
     // UIImages should only be accessed from the main thread 

     UIImage *img =testImage; 
     imgSize = [img size]; // this size will be pre rotated 
     orientation = [img imageOrientation]; 
     cgImg = CGImageRetain([img CGImage]); // this data is not rotated 
    }; 
    if([NSThread isMainThread]) { 
     createStartImgBlock(); 
    } else { 
     dispatch_sync(dispatch_get_main_queue(), createStartImgBlock); 
    } 
    CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB(); 
    // in iOS4+ you can let the context allocate memory by passing NULL 
    CGContextRef context = CGBitmapContextCreate(NULL, 
               imgSize.width, 
               imgSize.height, 
               8, 
               imgSize.width * 4, 
               colorspace, 
               kCGImageAlphaPremultipliedLast); 
    // rotate so the image respects the original UIImage's orientation 
    switch (orientation) { 
     case UIImageOrientationDown: 
      CGContextTranslateCTM(context, imgSize.width, imgSize.height); 
      CGContextRotateCTM(context, -radians); 
      break; 
     case UIImageOrientationLeft: 
      CGContextTranslateCTM(context, 0.0, imgSize.height); 
      CGContextRotateCTM(context, 3.0 * -radians/2.0); 
      break; 
     case UIImageOrientationRight: 
      CGContextTranslateCTM(context,imgSize.width, 0.0); 
      CGContextRotateCTM(context, -radians/2.0); 
      break; 
     default: 
      // there are mirrored modes possible 
      // but they aren't generated by the iPhone's camera 
      break; 
    } 
    // rotate the image upside down 

    CGContextTranslateCTM(context, +(imgSize.width * 0.5f), +(imgSize.height * 0.5f)); 
    CGContextRotateCTM(context, -radians); 
    //CGContextDrawImage(context, CGRectMake(0.0, 0.0, imgSize.width, imgSize.height), cgImg); 
    CGContextDrawImage(context, (CGRect){.origin.x = -imgSize.width* 0.9f , .origin.y = -imgSize.width* 0.5f , .size.width = imgSize.width, .size.height = imgSize.width}, cgImg); 
    // grab the new rotated image 
    CGContextFlush(context); 
    CGImageRef newCgImg = CGBitmapContextCreateImage(context); 
    __block UIImage *newImage; 
    dispatch_block_t createRotatedImgBlock = ^(void) { 
     // UIImages should only be accessed from the main thread 
     newImage = [UIImage imageWithCGImage:newCgImg]; 
    }; 
    if([NSThread isMainThread]) { 
     createRotatedImgBlock(); 
    } else { 
     dispatch_sync(dispatch_get_main_queue(), createRotatedImgBlock); 
    } 
    CGColorSpaceRelease(colorspace); 
    CGImageRelease(newCgImg); 
    CGContextRelease(context); 
    return newImage; 
} 

画像の回転にはうまくいきますが、回転するたびに画像と画像が縮小されます。

誰かが私を助けることができますか?

+0

参照[ここ](https://stackoverflow.com/questions/23615562/uiimageview-rotation-without-moving-image) – Kerberos

答えて

0

回転をアニメーション化する場合は、Core Animationを使用できます。これはイメージ自体を変更することはなく、画面上で回転させるだけです。

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; 
rotationAnimation.toValue = [NSNumber numberWithFloat:M_PI * 2.0]; 
rotationAnimation.duration = duration; 
rotationAnimation.cumulative = YES; 
rotationAnimation.repeatCount = numRotations; 
[self.imgviewRotate.layer addAnimation:animation forKey:@"animation"]; 
+0

私がボタンを持っている、と私はそれを小娘とき、私は、画像を回転させます。ユーザーは彼が望むすべての時間を回転させることができます。私はimageview.imageを回転させる必要がありますが、比率を失うことはありません。 – Jota

+0

私が提案したようにビューを回転させる際の問題は何ですか? – LGP

+0

申し訳ありませんが、最後に問題は別の場所にありました。私はいつも回転をよくしていました。 – Jota