ios 代理实例和从iPhone的photo中取得图片

邓宜年
2023-12-01
#import "controller.h"

@implementation controller

-(id)init
{
    [super viewDidLoad];
    
    
    //创建选取相片的button
    m_button=[[UIButton alloc]initWithFrame:CGRectMake(20, 40, 100, 30)];
    [m_button setTitle:@"选取相片" forState:UIControlStateNormal];
    [m_button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    m_button.backgroundColor=[UIColor blackColor];
    [m_button addTarget:self action:@selector(insertImage:) forControlEvents:UIControlEventTouchDown];
    
    
    //创建一个用于修改图片信息的button
    m_button1=[[UIButton alloc]initWithFrame:CGRectMake(180, 40, 120, 30)];
    [m_button1 setTitle:@"修改图片信息" forState:UIControlStateNormal];
    [m_button1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    m_button1.backgroundColor=[UIColor blackColor];
    [m_button1 addTarget:self action:@selector(changeImage:) forControlEvents:UIControlEventTouchDown];
    
    //创建一个UIimageView,用于显示选择后的图片
    m_image = [[UIImageView alloc]initWithFrame:CGRectMake(0, 90, 320, 320)];
    
    //创建一个UILabel,用于显示修改图片的信息
    m_label = [[UILabel alloc]initWithFrame:CGRectMake(30, 420, 200, 40)];
    
    
    
     [self.view addSubview:m_label];
    [self.view addSubview:m_image];
    [self.view addSubview: m_button];
    [self.view addSubview: m_button1];
    
    return  self;
}
-(void)inputWordsViewController:(NSString *)text
{
        m_label.text=text;
   
    [self dismissViewControllerAnimated:YES completion:Nil];
}

-(void)dealloc
{
    [m_button1 release];
    [m_button release];
    [m_image release];
}
-(void)changeImage:(id)sender
{
    

    InputWordsViewController *inputwords=[[InputWordsViewController alloc]init];
    inputwords.delegate=self;
    AppDelegate *appdelegate=(AppDelegate *)[[UIApplication sharedApplication]delegate];
    UINavigationController *m_navigation=appdelegate.m_navigation;
    [self presentViewController:inputwords animated:YES completion:nil];
    [inputwords release];
    
}


-(void)insertImage:(id)sender
{
    UIImagePickerController *m_piccontrol = [[UIImagePickerController alloc]init];
    
    //用于回调选中的图片
    m_piccontrol.delegate=self;
    
    
    m_piccontrol.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
    AppDelegate *appdelegate=(AppDelegate *)[[UIApplication sharedApplication]delegate];
//    UINavigationController *m_navigation=appdelegate.m_navigation;
    [self presentViewController:m_piccontrol animated:YES completion:nil];
}


//当图片被选中是执行下面的方法
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
    
    //表示把所选中的image传到UIImageView中去再由delegate去实现
    m_image.image=image;
    [self dismissModalViewControllerAnimated:YES];
    NSLog(@"imagePickerController");
}

//当点击cancle的时候执行下面的方法

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [self dismissViewControllerAnimated:YES completion:nil];
NSLog(@"imagePickerControllerDidCancel");
}
@end





#import "InputWordsViewController.h"

@implementation InputWordsViewController
@synthesize m_textfield;

@synthesize delegate;
-(void) doneInput:(id)sender
{

    if([self.delegate respondsToSelector:@selector(inputWordsViewController:)])
    {
        [self.delegate inputWordsViewController:m_textfield.text];
    }
}

-(id)init
{
    
    
    NSAutoreleasePool *m_autorelease= [[NSAutoreleasePool alloc]init];
    
    
         m_textfield= [UITextField alloc];
        [m_textfield init];
        [m_textfield release];
        NSLog(@"retainCount is %u",m_textfield.retainCount);
        m_textfield.frame=CGRectMake(20, 40, 280, 40);
        NSLog(@"retainCount is %u",m_textfield.retainCount);
        m_textfield.textColor=[UIColor blackColor];
        NSLog(@"retainCount is %u",m_textfield.retainCount);
        m_textfield.borderStyle=UITextBorderStyleRoundedRect;
        NSLog(@"retainCount is %u",m_textfield.retainCount);
        [self.view addSubview:m_textfield];
        [m_textfield release];
        NSLog(@"retainCount is %u",m_textfield.retainCount);

    
    [NSThread detachNewThreadSelector:@selector(thread) toTarget:self withObject:nil];
    [NSThread sleepForTimeInterval:4];
    
    NSOperation *m_operation= [[NSOperation alloc]init];
    
    m_button = [[UIButton alloc]initWithFrame:CGRectMake(200, 100, 100, 40)];
    [m_button setTitle:@"输入完毕" forState:UIControlStateNormal];
    [m_button setBackgroundColor:[UIColor blackColor]];
    [m_button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [m_button addTarget:self action:@selector(doneInput:) forControlEvents:UIControlEventAllEvents];
    [self.view addSubview:m_button];
    return self;
}
-(void)thread
{
//    NSLog(@"thread");
//    [ self performSelectorOnMainThread:@selector(performSelectorOnMainThread1) withObject:self waitUntilDone:YES];
}
-(void)performSelectorOnMainThread1
{
//    NSLog(@"performSelectorOnMainThread1");
}
-(void)dealloc
{
   [m_textfield release];
   
    [m_button release];
   
    
}

@end


 类似资料: