gpt4 book ai didi

ios - 我的图像没有被裁剪

转载 作者:行者123 更新时间:2023-11-28 22:54:48 25 4
gpt4 key购买 nike

我有以下主要从 SO 获得的代码,但我的图像仍然没有被裁剪,我做错了什么?

@synthesize TopImage;
@synthesize BottomImage;
@synthesize RotaterImage;

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
TopImage = [UIImage imageNamed:@"zero.png"];
[TopImage drawAtPoint:CGPointMake(0, 0)];
[self cropImage:TopImage:0];
[self addSubview:[[UIImageView alloc] initWithImage:TopImage]];
// BottomImage = [UIImage imageNamed:@"zero.png"];
// [BottomImage drawAtPoint:CGPointMake(0, 55)];
// [self cropImage:BottomImage:50];
// [self addSubview:[[[UIImageView alloc] initWithImage:BottomImage]retain]];
// RotaterImage = [UIImage imageNamed:@"zero.png"];
// [RotaterImage drawAtPoint:CGPointMake(0, 0) blendMode:kCGBlendModeNormal alpha:0];
// [self addSubview:[[[UIImageView alloc] initWithImage:RotaterImage]retain]];
// [self cropImage:RotaterImage:50];
}
return self;
}

-(void)cropImage:(UIImage *)image:(int)startCroppingPosition{
CGRect tempRect = CGRectMake(0, startCroppingPosition, 23, 40);
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], tempRect);
// or use the UIImage wherever you like
image = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
}

我是 IOS 的新手,所以感谢任何帮助

最佳答案

你正在创建一个新的裁剪图像但没有告诉 init 方法:)

试试这个改变:

-(UIImage *)cropImage:(UIImage *)image:(int)startCroppingPosition{
CGRect tempRect = CGRectMake(0, startCroppingPosition, 23, 40);
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], tempRect);
// or use the UIImage wherever you like
UIImage *newImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return newImage;
}

并且,在 init 中,替换

[TopImage drawAtPoint:CGPointMake(0, 0)];

UIImageView* topImageView = [[UIImageView alloc] initWithImage:topImage];
topImageView.frame = CGRectMake(x,y,width,height);

您的裁剪方法现在返回新裁剪的图像并将其存储在 TopImage 中。


其他建议:

类名以大写字母开头,变量以小写字母开头。 TopImage 应该是 topImage

此外,我总是会在您的方法中使用命名参数,所以

-(void)cropImage:(UIImage *)image:(int)startCroppingPosition

应该是这样的

-(void)cropImage:(UIImage *)image startPosition:(int)startCroppingPosition

这将使您的代码在一个月后回过头来时更具可读性(我在过去的艰难过程中学到了这一点!)

关于ios - 我的图像没有被裁剪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11056167/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com