简体   繁体   English

将图像从UIImageView复制到UIView

[英]Copy Image from UIImageView into UIView

I wish to copy an image from the UIImageView to UIView. 我希望将图像从UIImageView复制到UIView。

I have written the swipe gesture for the UIImageView to select images and need to know how to duplicate multiple images from UIImageView to UIView when user click the Add button. 我已经为UIImageView编写了滑动手势以选择图像,并且需要知道如何在用户单击“添加”按钮时将多个图像从UIImageView复制到UIView。 I want user to be able to duplicate different image into the UIView. 我希望用户能够将不同的图像复制到UIView中。 I will be able to arrange it later. 我稍后可以安排。

- (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 Design 图形界面设计

在此处输入图片说明

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

myview.backgroundColor = [UIColor colorWithPatternImage:myimage];

This is my take on this: 这是我的看法:

First, you are unnecessarily adding images in your images array every time inside handleSwipe: . 首先,每次在handleSwipe:中不必要地在images数组中添加图像。 Make images a class level property and add your images once, probably in loadView: method. images设为类级别的属性,然后一次添加图像(可能在loadView:方法中)。

Next, keep hold on to your current imageIndex . 接下来,保持当前的imageIndex不变。 Move this to class level property if not already there. 如果尚不存在,请将其移至类级别的属性。

Finally, in your view, add your selected image like this: 最后,在您的视图中,添加您选择的图像,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM