1

我试图弄清楚如何调整加载有标签的 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;

}

资料来源:调整 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 的大小。

4

2 回答 2

1

在您的 case 0: 子句中,touch.view 将是带有标签 0 的图像,因此只需将该视图作为参数调用您的调整大小方法:

UIImage *resizedImage = [self  imageWithImage:(UIImage *)touch.view scaledToSize:(CGSize)newSize];

您需要将方法更改为实例方法才能使其工作:

- (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;
于 2013-07-13T15:33:05.040 回答
0

I guess you ask how to get the image. From your code, you first need get the tagged view which is the DragView instance, and then you can get the image simply by setting the image as a property of DragView.

于 2013-07-13T14:52:01.863 回答