如何在模板图像中的特定区域添加图像

时间:2016-07-25 05:28:22

标签: ios objective-c iphone xcode core-graphics

这是一个模板图像(http://i.stack.imgur.com/9ME6A.jpg),其中有多个圆形区域可用。我们需要,当我们点击任何圆形区域(逐个)时,画廊应该是打开的,并且在从图库中选择图像后,应该在所选圆形区域上显示图像。        请提供我的问题的任何链接或演示。我在这里坚持。

谢谢!

3 个答案:

答案 0 :(得分:0)

  1. 使用5" UIImageView"创建一个viewController。观点。根据需要安排
  2. 放置你的笑脸图片。
  3. 将UITapGestureRecognizer添加到每个UIImageView中并捕获它们
  4. 打开UIImagePicker并将您的VC作为代理
  5. 抓住用户选择的图片事件并将图像放置到具体的UIImageView

答案 1 :(得分:0)

您应该从后端获取模板图像中圆形图像的映射(位置和尺寸),并使用这些值显示圆形并从图库中选择图像。

如果您获得包含所有圆圈的单个图像,则在单个图像中识别所有圆圈(使用复杂算法)将非常困难。

如果后端能够提供映射数据,则第一种方法是正确的方法。

答案 2 :(得分:0)

ViewController.h中的

#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
@interface ViewController : UIViewController <UINavigationControllerDelegate,UIImagePickerControllerDelegate,UIGestureRecognizerDelegate>
@property (strong, nonatomic) IBOutlet UIImageView *imageView1;
@property (strong, nonatomic) IBOutlet UIImageView *imageView2;
@property (strong, nonatomic) IBOutlet UIImageView *imageView3;
@property (strong, nonatomic) IBOutlet UIImageView *imageView4;
@property (strong, nonatomic) IBOutlet UIImageView *imageView5;
@end

ViewController.m

#import "ViewController.h"
@interface ViewController ()
{
  UIImageView *imageviewPick;
}
@end
@implementation ViewController
@synthesize imageView1,imageView2,imageView3,imageView4,imageView5;


- (void)viewDidLoad
{
 [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

imageView1.layer.cornerRadius = self.imageView1.frame.size.height / 2;
imageView1.layer.borderWidth = 1.0f;
imageView1.layer.borderColor = [[UIColor grayColor] CGColor];
imageView1.layer.masksToBounds = YES;

imageView2.layer.cornerRadius = self.imageView2.frame.size.height / 2;
imageView2.layer.borderWidth = 1.0f;
imageView2.layer.borderColor = [[UIColor grayColor] CGColor];
imageView2.layer.masksToBounds = YES;

imageView3.layer.cornerRadius = self.imageView3.frame.size.height / 2;
imageView3.layer.borderWidth = 1.0f;
imageView3.layer.borderColor = [[UIColor grayColor] CGColor];
imageView3.layer.masksToBounds = YES;

imageView4.layer.cornerRadius = self.imageView4.frame.size.height / 2;
imageView4.layer.borderWidth = 1.0f;
imageView4.layer.borderColor = [[UIColor grayColor] CGColor];
imageView4.layer.masksToBounds = YES;

imageView5.layer.cornerRadius = self.imageView5.frame.size.height / 2;
imageView5.layer.borderWidth = 1.0f;
imageView5.layer.borderColor = [[UIColor grayColor] CGColor];
imageView5.layer.masksToBounds = YES;



UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapImageOne:)];
tap1.numberOfTapsRequired = 1;

UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapImageTwo:)];
tap2.numberOfTapsRequired = 1;

UITapGestureRecognizer *tap3 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapImageThree:)];
tap3.numberOfTapsRequired = 1;

UITapGestureRecognizer *tap4 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapImageFour:)];
tap4.numberOfTapsRequired = 1;

UITapGestureRecognizer *tap5 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapImageFive:)];
tap4.numberOfTapsRequired = 1;


imageView1.userInteractionEnabled = YES;
imageView2.userInteractionEnabled = YES;
imageView3.userInteractionEnabled = YES;
imageView4.userInteractionEnabled = YES;
imageView5.userInteractionEnabled = YES;


[imageView1 addGestureRecognizer:tap1];
[imageView2 addGestureRecognizer:tap2];
[imageView3 addGestureRecognizer:tap3];
[imageView4 addGestureRecognizer:tap4];
[imageView5 addGestureRecognizer:tap5];
}
- (void)didReceiveMemoryWarning 
{
  [super didReceiveMemoryWarning];
  // Dispose of any resources that can be recreated.
}

-(void)tapImageOne:(UIGestureRecognizer *)tapGesture
{
  imageviewPick = (UIImageView *)tapGesture.view;
  [self showGallery];
}
-(void)tapImageTwo:(UIGestureRecognizer *)tapGesture
{
  imageviewPick = (UIImageView *)tapGesture.view;
  [self showGallery];
}
-(void)tapImageThree:(UIGestureRecognizer *)tapGesture
{
   imageviewPick = (UIImageView *)tapGesture.view;
   [self showGallery];
}
-(void)tapImageFour:(UIGestureRecognizer *)tapGesture
{
  imageviewPick = (UIImageView *)tapGesture.view;
  [self showGallery];
}
-(void)tapImageFive:(UIGestureRecognizer *)tapGesture
{
  imageviewPick = (UIImageView *)tapGesture.view;
  [self showGallery];
}

-(void)showGallery
{
   UIImagePickerController *pickImage = [[UIImagePickerController alloc]init];
   pickImage.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
   pickImage.delegate = self;
   [self presentViewController:pickImage animated:YES completion:nil];
}

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
   UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
   imageviewPick.image = image;
   [picker dismissViewControllerAnimated:YES completion:nil];
}
@end