【问题标题】:Resize a tagged UIImage调整标记的 UIImage 的大小
【发布时间】: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


【解决方案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;

【讨论】:

  • 我再次遇到了以下消息“+[UIImage imageWithImage:scaledToSize:]: unrecognized selector sent to class 0xa74154 2013-07-14 06:49:28.320 DragTest4[23192:a0b] ] *** 由于未捕获的异常 'NSInvalidArgumentException' 导致应用程序终止,原因:'+[UIImage imageWithImage:scaledToSize:]: unrecognized selector sent to class 0xa74154'"
  • @PeterK,有几种方法可以解决这个问题。您可以在 UIImage 上创建一个类别并将 imageWithImage:scaledToSize: 方法放在那里。另一种方法是使其成为实例方法(而不是类)。我已经更新了我的答案以表明这一点。
  • 我仍然遇到同样的问题。我相信“(UIImage *)touch.view”(标签)是问题,因为当我直接引用图像进行测试时它会起作用。
  • @PeterK,还有什么问题?如果您尝试我的代码,您会看到什么结果?
【解决方案2】:

我猜你问如何获取图像。 从您的代码中,您首先需要获取标记视图,即 DragView 实例,然后您只需将图像设置为 DragView 的属性即可获取图像。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-17
    • 1970-01-01
    • 2015-11-20
    • 2013-01-16
    • 2012-02-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多