0
CALayer *featureLayer = nil;
[featureLayer setContents:(id)[square CGImage]];

我需要从数组图像内部更改触摸时的“正方形”。

- (void)viewDidLoad
{
    [super viewDidLoad];
images = [[NSArray alloc] initWithObjects:
              [UIImage imageNamed:@"ic copy2"],
              [UIImage imageNamed:@"bubble 1"],
              [UIImage imageNamed:@"bubble 2"],
              [UIImage imageNamed:@"bubble 3"],
              [UIImage imageNamed:@"bubble 4"],
              nil];
square=[images objectAtIndex:0];
//some code....}

这是在触摸事件上更改图像的方法。

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [[event allTouches] anyObject];
      NSLog(@"'Touches Began");

    if ([touch view] == previewView) {
        square = [images objectAtIndex:1];
        NSLog(@"Image touched and changed");
    }

    currentImage++;
    NSLog(@"Touch Count = %d", currentImage);
    if (currentImage == [images count])
        currentImage = 0;

    square= [images objectAtIndex:currentImage];



}

由于 Setcontents 制作图像的静态副本,因此图像在 uiview 中不会改变。有什么方法可以从图像数组中更改触摸时添加内容的图像。?

4

1 回答 1

0

您似乎对指针有点误解。

当您第一次分配时contents = square contents,是指针地址的副本(不是对象副本)。这意味着以后更改 的地址square并不会同时修改contents. 即使它们指向相同的地址,它们也是独立的指针。contents修改指针后需要重新分配square指针。

square = [images objectAtIndex:currentImage];
[featureLayer setContents:(id)[square CGImage]];
于 2013-08-31T15:31:51.803 回答