iOS将图像组合到Parse.com

时间:2014-12-11 15:52:04

标签: ios image blend

我希望将两个图像组合在一起并将它们作为一个图像发送到Parse数据库。 只有我没有得到任何图像。

这是我的代码: .h文件

#import <UIKit/UIKit.h>
#import <Parse/Parse.h>


@interface mailViewController : UIViewController<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *naamText;
@property (weak, nonatomic) IBOutlet UITextField *emailText;
@property (weak, nonatomic) IBOutlet UIImageView *dankView;
@property (weak, nonatomic) IBOutlet UILabel *label;
@property (weak, nonatomic) IBOutlet UIButton *buttonLogo;
@property (weak, nonatomic) IBOutlet UIImageView *mailImage;
@property (weak, nonatomic) IBOutlet UIImageView *achtergrondImage;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@property (nonatomic, strong) UIImage *coverImage;
@property (nonatomic, strong) UIImage *achtergrond;
//@property (nonatomic, strong) UIImage *finalImage;

 - (IBAction)send:(id)sender;
 - (IBAction)cancel:(id)sender;

 @end

.m文件

  #import "mailViewController.h"
  #import "cat01ViewController.h"

@interface mailViewController ()

@property (nonatomic, strong) UIImage *finalImage;

@end

@implementation mailViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

   - (void)viewDidLoad
    {

    self.naamText.delegate = self;
    self.emailText.delegate = self;

    self.mailImage.image = self.coverImage;


    //hide dankwoord
    self.dankView.hidden = YES;
    _label.hidden = YES;
    _buttonLogo.hidden = YES;


    [super viewDidLoad];


}

- (void) viewWillAppear:(BOOL)animated
{
      if (self.coverImage != nil)
        self.mailImage.image = self.coverImage;
}

- (void)addImages{

    UIImage *image1 = self.mailImage.image;
    UIImage *image2 = self.achtergrond;

    CGSize size = CGSizeMake(image1.size.width, image1.size.height + image2.size.height);

    [image1 drawInRect:CGRectMake(0,0,size.width, image1.size.height)];
    [image2 drawInRect:CGRectMake(0,image1.size.height,size.width, image2.size.height)];

    self.finalImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    //Add image to view
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0,               self.finalImage.size.width, self.finalImage.size.height)];
    imageView.image = self.finalImage;
    [self.view addSubview: imageView];

}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

    [self.naamText resignFirstResponder];
    [self.emailText resignFirstResponder];
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField{

   if (textField) {
        [textField resignFirstResponder];
    }

    PFObject *emailadres = [PFObject objectWithClassName:@"emailadres"];
    [emailadres setObject:_naamText.text forKey:@"name"];
    [emailadres setObject:_emailText.text forKey:@"email"];
    emailadres[@"iPad_nr"] = @"iPad 2";
    [emailadres saveInBackground];

    self.dankView.hidden = NO;
    _label.hidden = NO;
    _buttonLogo.hidden = NO;


    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 5 * NSEC_PER_SEC),   dispatch_get_main_queue(), ^{
       _label.hidden = YES;
    });

    return NO;
}

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

- (IBAction)send:(id)sender {

    [self.naamText resignFirstResponder];
    [self.emailText resignFirstResponder];

    UIImage *image = self.finalImage;

    NSData *imageData = UIImagePNGRepresentation(image);
    PFFile *imageFile = [PFFile fileWithName:@"image.png" data:imageData];

    PFObject *mailPhoto = [PFObject objectWithClassName:@"mailPhoto"];
    mailPhoto[@"imageName"] = @"Ikzieikzie";
    mailPhoto[@"imageFile"] = imageFile;
    [mailPhoto setObject:_naamText.text forKey:@"name"];
    [mailPhoto setObject:_emailText.text forKey:@"email"];
    mailPhoto[@"iPad_nr"] = @"iPad 1";
    [mailPhoto saveInBackground];

    self.dankView.hidden = NO;
    _label.hidden = NO;
    _buttonLogo.hidden = NO;


    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 5 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
        _label.hidden = YES;
    });

}

- (IBAction)cancel:(id)sender {

    [self.naamText resignFirstResponder];
    [self.emailText resignFirstResponder];

    self.dankView.hidden = NO;
    _label.hidden = NO;
    _buttonLogo.hidden = NO;


    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 5 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
        _label.hidden = YES;
    });

}

@end

所以,我知道如何将图像单独发送到Parse数据库......但我不明白如何将它们组合并将它们作为一个发送。

3 个答案:

答案 0 :(得分:0)

您在finalImage中设置了局部变量addImages,您从未设置类变量self.finalImage来包含组合图像,因此这一行

UIImage *image = self.finalImage;

可能会将image设置为nil。

此外,您永远不会在UIImageView *imageView中添加addImages作为您可能想要的子视图。

因此,为了修复此代码,我建议在类的顶部创建定义finalImage作为类属性,例如:

@interface ViewController ()

@property (nonatomic, strong) UIImage *finalImage;

@end

然后在addImages中,您可以将局部变量finalImage更改为self.finalImage并正确添加UIImageView作为子视图,例如:

self.finalImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

//Add image to view
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.finalImage.size.width, self.finalImage.size.height)];
imageView.image = self.finalImage;
[self.view addSubview: imageView];

进行这些更改后,您仍然可以在send:中使用此行:

UIImage *image = self.finalImage; 

答案 1 :(得分:0)

你错放了上下文,它应该专注于imageview对象而不是图像对象。 试试这个:

UIImage *image1 = self.mailImage.image;
UIImage *image2 = self.achtergrond;

CGSize size = CGSizeMake(image1.size.width, image1.size.height + image2.size.height);

[image1 drawInRect:CGRectMake(0,0,size.width, image1.size.height)];
[image2 drawInRect:CGRectMake(0,image1.size.height,size.width, image2.size.height)];

UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();

//Add image to view
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, finalImage.size.width, finalImage.size.height)];

UIGraphicsBeginImageContext(size);

imageView.image = finalImage;

UIGraphicsEndImageContext();

希望这有帮助。

答案 2 :(得分:0)

所以,这是最终的解决方案!谢谢你的帮助!

UIImage *image1 = self.achtergrondImage.image;
UIImage *image2 = self.mailImage.image;

UIGraphicsBeginImageContext(image1.size);
[image1 drawInRect:CGRectMake(0, 0, image1.size.width, image1.size.height)];
[image2 drawInRect:CGRectMake(image1.size.width - image2.size.width, image1.size.height - image2.size.height, image2.size.width, image2.size.height)];

self.finalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.finalImage.size.width, self.finalImage.size.height)];
imageView.image = self.finalImage;