调整标记的UIImage的大小

时间:2013-07-12 22:06:18

标签: objective-c ios6 uiimage

我试图找出如何调整加载了标记的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?(Paul Lynch)

这是我添加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的大小。

2 个答案:

答案 0 :(得分:1)

在你的case 0:子句中,touch.view将是带有标记0的图像,所以只需用该视图作为参数调用你的resizing方法:

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;

答案 1 :(得分:0)

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