从UIImagePickerController到UIImage image =的图像

时间:2014-02-10 11:46:49

标签: ios uiimageview imageview uiimagepickercontroller

尝试将相机/相册照片加载到“添加边框”循环中,但无法使其正常工作。

该应用程序基本上拍摄照片或从相册中选择一个,然后允许用户在4个不同的边框之间进行选择,但是当用户切换边框时,它不会恢复到原始照片,它只是将边框分层在顶部。 / p>

我尝试过使用

UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];

但它不喜欢'info'

以下代码。

//  ABViewController.m

#import "ABViewController.h"
#import <QuartzCore/QuartzCore.h>
@interface ABViewController ()
@property (nonatomic) IBOutlet UIImageView *imageView;
@property (nonatomic) IBOutlet UIButton *border1;
@property (nonatomic) IBOutlet UIButton *border2;
@property (nonatomic) IBOutlet UIButton *border3;
@property (nonatomic) IBOutlet UIButton *border4;

@end

@implementation ABViewController

@synthesize imageView;

@synthesize border1, border2, border3, border4;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    border1.tag =1;
    border2.tag =2;
    border3.tag =3;
    border4.tag =4;
   imageView.image = [UIImage imageNamed:@"a7qIB4OiXsz5Q0BG4ipZV0FLF5_jB71p24h7Oa2ExYU.jpg"];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//Border code

- (IBAction)borderButtonTapped:(UIButton *)sender {



    UIImage* image=(UIImage*)[@"UIImagePickerControllerOriginalImage"];

    UIImage *borderImage = [UIImage imageNamed:[NSString stringWithFormat:@"borderImage%i.png", sender.tag]];

    NSData *dataFromImage = UIImageJPEGRepresentation(imageView.image, 1);

    CIImage *beginImage= [CIImage imageWithData:dataFromImage];

    CIContext *context = [CIContext contextWithOptions:nil];
    CIImage *border =[CIImage imageWithData:UIImagePNGRepresentation(borderImage)];

    CIFilter *filter= [CIFilter filterWithName:@"CISourceOverCompositing"];  //@"CISoftLightBlendMode"];
    [filter setDefaults];
    [filter setValue:border forKey:@"inputImage"];

    [filter setValue:beginImage forKey:@"inputBackgroundImage"];

    CIImage *outputImage = [filter valueForKey:@"outputImage"];
    CGImageRef cgimg = [context createCGImage:outputImage fromRect:[outputImage extent]];
    UIImage *newImg = [UIImage imageWithCGImage:cgimg];

    imageView.image = newImg;



}

// Camera CODE


- (IBAction)TakePhoto {
    picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    [picker setSourceType:UIImagePickerControllerSourceTypeCamera];
    [picker setShowsCameraControls:YES];
    [picker setAllowsEditing:YES];
    [self presentViewController:picker animated:YES completion:NULL];
    [picker release];
}

- (IBAction)ChooseExisting {
    picker2 = [[UIImagePickerController alloc] init];
    picker2.delegate = self;
    [picker2 setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
    [picker2 setAllowsEditing:YES];
    [self presentViewController:picker2 animated:YES completion:NULL];
    [picker2 release];
}

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    image = [info objectForKey:UIImagePickerControllerEditedImage];
    [imageView setImage:image];
    [self dismissViewControllerAnimated:YES completion:NULL];
    }

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [self dismissViewControllerAnimated:YES completion:NULL];
}


@end

1 个答案:

答案 0 :(得分:0)

尝试以下方法......

创建NSDictionary引用以保留info

@implementation ABViewController{
    NSDictionary *imageInfo;
}

imagePickerController: didFinishPickingMediaWithInfo:方法中添加:

imageInfo = info;

borderButtonTapped方法中,替换:

UIImage* image=(UIImage*)[@"UIImagePickerControllerOriginalImage"];

使用:

UIImage *image = [imageInfo valueForKey:UIImagePickerControllerOriginalImage];

修改

并改变:

NSData *dataFromImage = UIImageJPEGRepresentation(imageView.image, 1);

为:

NSData *dataFromImage = UIImageJPEGRepresentation(image, 1);