将图像从UIImageView复制到UIView中

时间:2015-10-08 06:28:35

标签: ios objective-c uiview uiimageview

我希望将图像从UIImageView复制到UIView。

我已经为UIImageView编写了滑动手势来选择图像,并且当用户单击“添加”按钮时,需要知道如何将多个图像从UIImageView复制到UIView。我希望用户能够将不同的图像复制到UIView中。我稍后可以安排。

- (IBAction)handleSwipe:(UIGestureRecognizer *)sender {
    //NSLog(@"swipe");

    NSArray *images=[[NSArray alloc] initWithObjects:
                     @"all_LE2_Series_capacitor.jpg",
                     @"all_LE2_Series_inductor.jpg",
                     @"all_LE2_Series_msline.jpg",
                     @"all_LE2_Series_resistor.jpg",
                     @"all_LE2_Short_stub.jpg", nil];

    UISwipeGestureRecognizerDirection direction = [(UISwipeGestureRecognizer *) sender direction];

    switch (direction){
        case UISwipeGestureRecognizerDirectionLeft: imageIndex++;
            break;
        case UISwipeGestureRecognizerDirectionRight: imageIndex--;
            break;
        default:
            break;
    }
    imageIndex = (imageIndex < 0) ? ([images count] -1):
    imageIndex % [images count];
    imageView.image = [UIImage imageNamed:[images objectAtIndex:imageIndex]];
}

- (IBAction)Add:(UIButton *)sender {
    dropTarget.center = imageView.center;
    [self.view addSubview:dropTarget];
}

GUI设计

enter image description here

2 个答案:

答案 0 :(得分:1)

要更改UIView背景图片,您可以使用

myview.backgroundColor = [UIColor colorWithPatternImage:myimage];

答案 1 :(得分:1)

这是我对此的看法:

首先,每次在images内,您都会在handleSwipe:数组中不必要地添加图片。将images作为类级别属性并添加一次图像,可能是loadView:方法。

接下来,继续关注您当前的imageIndex。如果不存在,则将其移至类级别属性。

在您的视图中

最后,添加您选择的图片,如下所示:

UIImageView *selectedImageView = [[UIImageView alloc] initWithImage:self.images[self.imageIndex]];
[self.view addSubView:selectedImageView]