【发布时间】:2013-07-12 22:06:18
【问题描述】:
我试图弄清楚如何调整加载有标签的 UIImage 的大小。我已经成功加载了三张图片并标记了它们。
以下是我用来测试的“touchesEnded”代码,它会触发 NSLogs,因此代码可以正常工作。在调整大小测试中,我想在 UIImage tag=0 移动后调整它的大小,这就是为什么我将它放在“touchesEnded”中的原因。
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@">>> touchesEnded <<<");
UITouch *touch = [[event allTouches] anyObject];
endLocation = [[touches anyObject] locationInView:self];
switch ([touch view].tag) {
case 0:
NSLog(@"touchesEnded: 0");
// Resize call here
break;
case 1:
NSLog(@"touchesEnded: 1");
[[touch view] setCenter: CGPointMake(180, 400)];
break;
case 2:
NSLog(@"touchesEnded: 2");
[[touch view] setCenter: CGPointMake(10, 10)];
break;
default:
break;
}
}
我想调用这个方法,我认为它应该可以工作:
+ (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
//UIGraphicsBeginImageContext(newSize);
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
来源:The simplest way to resize an UIImage?(保罗林奇)
这就是我添加 UIImages 的方式:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSArray *cards = [[NSArray alloc]initWithObjects:@"img1.png", @"img2.png", @"img3.png",nil];
int x = 0;
for (NSString *theCards in cards) {
DragView *actualCards = [[DragView alloc] initWithImage:[UIImage imageNamed:theCards]];
actualCards.tag = x;
NSLog(@"Tag: %i", x);
x++;
[self.view addSubview:actualCards];
}
}
标签 1 和 2 用于其他测试。
但我只是不让它使用具有标签的 UIImage 工作,所以我想寻求一些帮助或指导如何调用该函数并更改 UIImage 的大小。
【问题讨论】:
-
什么不起作用?你有什么错误吗?
-
@rdelmar 我不明白如何使用标记的 UIImages 调用该方法?
标签: objective-c ios6 uiimage