10度旋转图像被截断

发布于 2024-07-30 04:27:30 字数 1200 浏览 2 评论 0原文

我旋转图像时:

UIImage *image = [UIImage imageNamed:@"doneBtn.png"];
CGImageRef imgRef = image.CGImage;

CGFloat width = CGImageGetWidth(imgRef);
CGFloat height = CGImageGetHeight(imgRef);

CGAffineTransform transform = CGAffineTransformIdentity;
CGRect bounds = CGRectMake(0, 0, width, height);
transform = CGAffineTransformRotate(transform, degreesToRadians(10));

UIGraphicsBeginImageContext(bounds.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextConcatCTM(context, transform);

CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0,0,width,height), imgRef);
UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

img.image=imageCopy;

当我显示图像时,我看到它在 10 度以下,但图像的边缘被切断。

有人知道如何解决这个问题吗? 这是用于澄清的图像。

替代文本 http://img197.imageshack.us/img197/7077/problemkfr.png< /a>

我昨天一整天都在尝试,但无法弄清楚。

另一个示例图像:

替代文本 http://img25.imageshack.us/img25/3370/ afbeelding6b.png

I rotate my image with:

UIImage *image = [UIImage imageNamed:@"doneBtn.png"];
CGImageRef imgRef = image.CGImage;

CGFloat width = CGImageGetWidth(imgRef);
CGFloat height = CGImageGetHeight(imgRef);

CGAffineTransform transform = CGAffineTransformIdentity;
CGRect bounds = CGRectMake(0, 0, width, height);
transform = CGAffineTransformRotate(transform, degreesToRadians(10));

UIGraphicsBeginImageContext(bounds.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextConcatCTM(context, transform);

CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0,0,width,height), imgRef);
UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

img.image=imageCopy;

When I display the image, I see it under my 10 degree but the image is cut off on the edges.

Does somebody know how to fix this? Here's an image for clarification.

alt text http://img197.imageshack.us/img197/7077/problemkfr.png

I have been trying all day yesterday, but can't figure it out.

Another example image:

alt text http://img25.imageshack.us/img25/3370/afbeelding6b.png

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

生生漫 2024-08-06 04:27:30

代码 IN SliderAppViewController.h

#import <UIKit/UIKit.h>

@interface SliderAppViewController : UIViewController {
    IBOutlet UIImageView *imgView;
    IBOutlet UISlider *mySlider;
}
@property (nonatomic, retain) IBOutlet UISlider *mySlider;
- (IBAction) sliderValueChanged:(id)sender;  

@end

代码 IN SliderApp.m

#import "SliderAppViewController.h"

@implementation SliderAppViewController
@synthesize mySlider;

- (IBAction) sliderValueChanged:(UISlider *)sender {  

    imgView.transform = CGAffineTransformMakeRotation(45);

} 
- (void)viewDidLoad {
    mySlider.minimumValue = 1.0;
    mySlider.maximumValue = 100.0;

        //other code
}

然后在 IB 中建立连接

CODE IN SliderAppViewController.h

#import <UIKit/UIKit.h>

@interface SliderAppViewController : UIViewController {
    IBOutlet UIImageView *imgView;
    IBOutlet UISlider *mySlider;
}
@property (nonatomic, retain) IBOutlet UISlider *mySlider;
- (IBAction) sliderValueChanged:(id)sender;  

@end

CODE IN SliderApp.m

#import "SliderAppViewController.h"

@implementation SliderAppViewController
@synthesize mySlider;

- (IBAction) sliderValueChanged:(UISlider *)sender {  

    imgView.transform = CGAffineTransformMakeRotation(45);

} 
- (void)viewDidLoad {
    mySlider.minimumValue = 1.0;
    mySlider.maximumValue = 100.0;

        //other code
}

then make connection in IB

恰似旧人归 2024-08-06 04:27:30

你的问题是你的 CALayer 的锚点,我这里没有示例代码,但是如果你使用视图 CALayer 的锚点属性,你应该能够让视图看起来像你想要的那样。 我稍后会尝试发布一些示例代码

Your problem here is the anchor of your CALayer, I dont have example code right here with me, but if you play with the anchor property of the view CALayer you should be able to get the view to look like you want. I will try and post some sample code later

习ぎ惯性依靠 2024-08-06 04:27:30

我现在解决了它,我必须更改边界的宽度/高度值和
CGContextDrawImage 中的 x/y

CGRect bounds = CGRectMake(0, 0, width+10, height+10);
CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(10,0,width,height), imgRef);

I solved it now, i have to change the width/height value of bounds and
the x/y in CGContextDrawImage

CGRect bounds = CGRectMake(0, 0, width+10, height+10);
CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(10,0,width,height), imgRef);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文