I want to combine two images and send them as one to a Parse database.
Only I get no image as result.
This is my code:
.h file
#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 file
#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
So, I know how to send the images individual to the Parse database... but I cant understand how to combine them and send them as one.
You set the local variable finalImage in addImages, you never set a class variable self.finalImage to contain the combined image, so this line
UIImage *image = self.finalImage;
probably sets image to nil.
Also, you never add the UIImageView *imageView in addImages as a subview as you've probably intended.
So to fix this code, I'd recommend creating defining finalImage as a class property at the top of the class, ex:
#interface ViewController ()
#property (nonatomic, strong) UIImage *finalImage;
#end
Then in addImages you can change the local variable finalImage to self.finalImage and properly add the UIImageView as a subview, ex:
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];
After making these changes you can still use this line in send::
UIImage *image = self.finalImage;
You have misplaced the context, it should be focused to imageview object not to the image object.
Try this:
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();
Hope this helps.
So, this is the final solution! Thanks for your help!
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;
Related
I have a UIImageView and a UIView (filled with smaller imageviews) all placed in a UIScrollView (for zooming).
To better help you understand, I have a map (ImageView) with fog(UIView with smaller imageviews) in the scrollview so I can zoom the map.
If I add the mapview and fogview to the self.view then the application works as it should, touching the fog at a point removes that fog image.
However, when I add the map and fog views to the scrollview nothing responds to touches.
I ultimately want the default behavior to be touching fog view will remove the fog at one point. Unless the user presses a button to zoom/pinch the view, which will zoom both the mapview and fogview.
The problem below is no touches are processed when I add the mapview and fogview to the scrollview. Currently there is no ability to press a button to pinch/zoom as I still don't have the default behavior I want.
Any help?
#interface ViewController () <UIImagePickerControllerDelegate, UINavigationControllerDelegate, UIActionSheetDelegate, UIGestureRecognizerDelegate, UIScrollViewDelegate>{
UIImage * fogImage;
NSUserDefaults * defaults;
bool canZoom;
}
#property (weak, nonatomic) IBOutlet UIButton *btnTakePicture;
#property (strong, nonatomic) UIButton * btnMenu;
#property (strong, nonatomic) UIButton * btnZoom;
#property (strong, nonatomic) UIScrollView* scrollView;
#property (strong, nonatomic) UIImageView *mapView;
#property (strong, nonatomic) UIView * fogView;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
fogImage = [UIImage imageNamed:#"fog1.jpg"];
self.mapView = [[UIImageView alloc] initWithFrame:CGRectZero];
self.fogView = [[UIView alloc] initWithFrame:CGRectZero];
_scrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
_scrollView.delegate = self;
_scrollView.minimumZoomScale = 0.75;
_scrollView.maximumZoomScale = 3.0;
[_scrollView addSubview:self.mapView];
[_scrollView addSubview:self.fogView];
[self.view addSubview:_scrollView];
self.mapView.hidden = YES;
self.fogView.hidden = YES;
self.scrollView.hidden = YES;
defaults = [NSUserDefaults standardUserDefaults];
canZoom = false;
}
- (void) viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
self.mapView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
self.fogView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
self.scrollView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
}
- (void) zoom:(id)sender{
canZoom = !canZoom;
}
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *chosenImage = info[UIImagePickerControllerOriginalImage];
self.btnTakePicture.hidden = YES;
self.btnMenu.hidden = NO;
self.btnZoom.hidden = NO;
[picker dismissViewControllerAnimated:YES completion:^(void) {
[self fillScreenWithFog];
self.mapView.image = chosenImage;
self.mapView.contentMode = UIViewContentModeScaleAspectFit;
self.mapView.hidden = NO;
self.fogView.hidden = NO;
_scrollView.hidden = NO;
_scrollView.userInteractionEnabled = YES;
self.scrollView.contentSize = self.mapView.frame.size;
}];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:NULL];
}
#pragma mark Fog Maker
- (void) removeFogAtPoint:(CGPoint)point{
NSLog(#"removeFogAtPoint %#", NSStringFromCGPoint(point));
CGRect fingerRect = CGRectMake(point.x - 5, point.y-5, 10, 10);
for(UIImageView *view in self.fogView.subviews){
CGRect subviewFrame = view.frame;
if(CGRectIntersectsRect(fingerRect, subviewFrame)){
[UIView animateWithDuration:1.25
animations:^{
view.alpha = 0;
}
completion:^(BOOL finished){
[view removeFromSuperview];
}];
}
}
}
- (void) fillScreenWithFog {
// do a loop to fill the screen with fog getNewSquare
}
- (UIImageView*) getNewSquare:(CGRect)frame withTag:(int)tag{
UIImageView * imageView = [[UIImageView alloc] initWithFrame:frame];
imageView.image = fogImage;
imageView.contentMode = UIViewContentModeScaleAspectFill;
return imageView;
}
-(void) touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
NSLog(#"Touches Moved");
CGPoint location = [[touches anyObject] locationInView:self.scrollView];
[self removeFogAtPoint:location];
}
#end
Interface is define like this
#interface IGLDemoCustomView : UIView
#property (nonatomic, strong) UIImage *image;
#property (nonatomic, strong) NSString *title;
#end
While Implementation file look like this
#interface IGLDemoCustomView ()
#property (nonatomic, strong) UIImageView *imageView;
#property (nonatomic, strong) UILabel *titleLabel
#end
#implementation IGLDemoCustomView
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self commonInit];
}
return self;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
[self commonInit];
}
return self;
}
- (void)commonInit
{
[self initView];
}
- (void)initView
{
self.layer.borderColor = [UIColor colorWithRed:0.18 green:0.59 blue:0.69 alpha:1.0].CGColor;
self.layer.borderWidth = 2.0;
self.layer.masksToBounds = YES;
self.backgroundColor = [UIColor whiteColor];
self.alpha = 0.8;
UIImageView *imageView = [[UIImageView alloc] init];
imageView.contentMode = UIViewContentModeCenter;
[self addSubview:imageView];
self.imageView = imageView;
UILabel *titleLabel;
self.titleLabel = [[UILabel alloc]init];
titleLabel.center = self.center; // set proper frame for label
[self addSubview:titleLabel];
}
- (void)setImage:(UIImage *)image
{
_image = image;
self.imageView.image = image;
}
- (void)setString:(NSString *)title
{
self.title=title;
self.titleLabel.text = title;
}
- (void)setFrame:(CGRect)frame
{
[super setFrame:frame];
self.imageView.frame = self.bounds;
self.layer.cornerRadius = frame.size.height / 2.0;
}
#end
When i select image from the drop down menu it shows in the menu, but when i select any text from the drop down menu it doesnt show in the drop down list view.
Any clue will be highly appreciated.
Calling and setting string in the view
IGLDropDownItem *menuButton = strongSelf.dropDownMenu.menuButton;
IGLDemoCustomView *buttonView = (IGLDemoCustomView*)menuButton.customView;
buttonView.title = device.name;
You need to set a frame for the label
- (void)setFrame:(CGRect)frame
{
[super setFrame:frame];
self.imageView.frame = self.bounds;
self.titleLabel.frame = self.bounds;
self.layer.cornerRadius = frame.size.height / 2.0;
}
If you are expecting the text to wrap, you'll need to set the lineBreakMode and set numberOfLines to 0
Like you are having UIImageView you need to have a UILabel inside your UIView too.
#interface IGLDemoCustomView ()
#property (nonatomic, strong) UIImageView *imageView;
#property (nonatomic, strong) UILabel *titleLabel;
#end
- (void)initView
{
self.layer.borderColor = [UIColor colorWithRed:0.18 green:0.59 blue:0.69 alpha:1.0].CGColor;
self.layer.borderWidth = 2.0;
self.layer.masksToBounds = YES;
self.backgroundColor = [UIColor whiteColor];
self.alpha = 0.8;
UIImageView *imageView = [[UIImageView alloc] init];
imageView.contentMode = UIViewContentModeCenter;
[self addSubview:imageView];
self.imageView = imageView;
self.titleLabel = [[UILabel alloc]init];
titleLabel.center = self.center; // set proper frame for label
[self addSubview:titleLabel]
}
And in setString
- (void)setString:(NSString *)title
{
self.title=title;
self.titleLabel.text = title;
}
This is a template image (http://i.stack.imgur.com/9ME6A.jpg) in which multiple circular area are available. we need, when we click on any circular area (one by one ) then gallery should be open and after selecting the image from gallery that image should be shown on the selected circular area.
Please provide me any link or demo for my problem. i am stucking here.
Thanks!
Make a viewController with 5 "UIImageView" views. Arrange as you need it
Place there your smile images.
Add UITapGestureRecognizer into every UIImageView and catch them
Open UIImagePicker and make your VC as a delegate
Catch user selected picture event and place the image to your concrete UIImageView
You should get the mapping(position and dimensions) of circular images in the template image from the backend and use those values for displaying circles and selecting images from gallery.
If you are getting a single image with all circles, then it will be very difficult to identify all the circles(using complex algorithms) in the single image.
If the backend is able to provide the mapping data, first method is the correct method.
in 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
I want to show an image using a UIImageView inside a UIScrollview. The image needs to fit on the screen while keeping the aspect ratio.
This all works, except that, when I zoom first, I can scroll below the image. The height of area is about 64.
This is how it looks after scrolling (White is the background color of the UIImageView and red is the background color of the UIScrollView):
This is the code in my ImageViewController:
#import "ImageViewController.h"
#interface ImageViewController () <UIScrollViewDelegate>
#property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
#property (nonatomic, strong) UIImageView *imageView;
#property (nonatomic, strong) UIImage *image;
#end
#implementation ImageViewController
- (void)viewDidLoad
{
NSLog(#"viewDidLoad");
[super viewDidLoad];
_scrollView.minimumZoomScale = 1.0 ;
_scrollView.maximumZoomScale = _imageView.image.size.width / _scrollView.frame.size.width;
_scrollView.zoomScale = 1.0;
_scrollView.backgroundColor = [UIColor redColor];
_scrollView.delegate = self;
_imageView.backgroundColor = [UIColor whiteColor];
[_scrollView addSubview:_imageView];
}
- (void)viewDidLayoutSubviews
{
self.imageView.frame = self.scrollView.bounds;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.imageView setContentMode:UIViewContentModeScaleAspectFit];
}
- (UIImageView *)imageView
{
if (!_imageView) _imageView = [[UIImageView alloc] init];
return _imageView;
}
- (UIImage *)image
{
NSLog(#"image");
return self.imageView.image;
}
- (void)setImage:(UIImage *)image
{
NSLog(#"setImage");
self.imageView.image = image;
}
#pragma mark - UIScrollViewDelegate
// mandatory zooming method in UIScrollViewDelegate protocol
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return self.imageView;
}
-(void)setImageURL:(NSURL *)imageURL
{
NSLog(#"setImageURL");
_imageURL = imageURL;
self.image = [UIImage imageWithContentsOfFile:[imageURL path]];
}
#end
This are the properties of my UIScrollView:
How can I fix this?
I got it working. This is my code:
#import "ImageViewController.h"
#interface ImageViewController () <UIScrollViewDelegate>
#property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
#property (nonatomic, strong) UIImageView *imageView;
#property (nonatomic, strong) UIImage *image;
#end
#implementation ImageViewController
- (void)viewDidLoad
{
NSLog(#"viewDidLoad");
[super viewDidLoad];
self.scrollView.contentSize = self.scrollView.frame.size;
self.scrollView.delegate = self;
CGRect rect = CGRectZero;
rect.size = self.image.size;
self.scrollView.minimumZoomScale = 0.5;
self.scrollView.maximumZoomScale = 2.5;
self.scrollView.zoomScale = 1.0;
self.imageView = [[UIImageView alloc] initWithFrame:rect];
self.imageView.image = self.image;
[self.scrollView addSubview:self.imageView];
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
}
- (IBAction)tappedAction:(id)sender {
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:#[self.image] applicationActivities:nil];
[self presentViewController:activityVC animated:YES completion:nil];
}
- (void)viewDidLayoutSubviews
{
self.imageView.frame = self.scrollView.bounds;
}
#pragma mark - UIScrollViewDelegate
// mandatory zooming method in UIScrollViewDelegate protocol
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return self.imageView;
}
- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{
UIView *subView = [scrollView.subviews objectAtIndex:0];
CGFloat offsetX = MAX((scrollView.bounds.size.width - scrollView.contentSize.width) * 0.5, 0.0);
CGFloat offsetY = MAX((scrollView.bounds.size.height - scrollView.contentSize.height) * 0.5, 0.0);
subView.center = CGPointMake(scrollView.contentSize.width * 0.5 + offsetX,
scrollView.contentSize.height * 0.5 + offsetY);
}
-(void)setImageURL:(NSURL *)imageURL
{
NSLog(#"setImageURL");
_imageURL = imageURL;
self.image = [UIImage imageWithContentsOfFile:[imageURL path]];
}
#end
The code in scrollViewDidZoom: is used to keep the image centered.
I am trying to call another method with Delegate and protocols in my app. I need to send the variable "myData" to another view but for some reasons it doesn't works. I don't know what I am doing wrong with the implementation of delegates and protocols. The delegate never call the action of the protocol.
Sorry I'm new with this.
BLEViewController.h
#import <UIKit/UIKit.h>
#import "BLE.h"
#protocol EnviarDatos <NSObject>
//Metodo que se manda llamar pero se implementa en otra clase
-(void) actualizaDatos:(NSData*)Data;
#end
#interface BLEViewController : UITableViewController <BLEDelegate>
{
//id <EnviarDatos> delegate;
}
#property (nonatomic,assign)id delegate;
#property (nonatomic, retain) NSData *myData;
+ (BLE*) theBLEObject;
- (void) scanForPeripherals;
- (IBAction)connect:(id)sender;
-(void) activaProtocolo;
#end
BLEViewController.m
//
// BLEViewController.m
// DL_RemoteBLE_02
//
// Created by Dave Lichtenstein on 3/16/14.
// Copyright (c) 2014 Dave Lichtenstein. All rights reserved.
//
#import "BLEViewController.h"
static BLE* ble;
static UILabel *statusLabel;
static NSString* connectionStatus = #"Not connected!";
#interface BLEViewController ()
#end
#implementation BLEViewController
#synthesize delegate;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
if(ble==nil)
{
// Create our Bluetooth Low Energy object
//
ble = [[BLE alloc] init];
[ble controlSetup];
ble.delegate = self;
}
// Create a toolbar at the bottom of the screen to show status text, etc.
//
// get screen size
//
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
CGFloat toolbarHeight = 50.0;
CGFloat labelHeight = 50.0;
if(statusLabel==nil) // only create once
{
// create our status label object
//
statusLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, screenHeight-toolbarHeight-labelHeight, screenWidth, labelHeight)];
statusLabel.backgroundColor = [UIColor clearColor];
statusLabel.textColor = [UIColor blackColor];
statusLabel.font = [UIFont boldSystemFontOfSize:15];
statusLabel.text = #"Connection Status:";
}
// create a toolbar
//
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0,screenHeight-toolbarHeight,screenWidth,toolbarHeight)];
toolbar.tintColor = [UIColor blackColor];
/*UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 150, 20)];
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor blackColor];
label.font = [UIFont boldSystemFontOfSize:15];
label.text = #"Status:";
UIBarButtonItem *labeltext = [[UIBarButtonItem alloc] initWithCustomView:label];
UIBarButtonItem *button = [[UIBarButtonItem alloc]initWithTitle:#"" style:UIBarButtonItemStyleDone target:self action:nil];
NSArray *items = [NSArray arrayWithObjects:statusLabel, nil];
toolbar.items = items;
*/
[self.view addSubview:statusLabel];
[self.view addSubview:toolbar];
// Update our status label
statusLabel.text = connectionStatus;
_myData = [[NSData alloc]init];
delegate = self;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//-------------------------------------------------------
// methods
/////////////////////////////////////////////////////////
+ (BLE*) theBLEObject
{
return ble;
}
-(void) connectionTimer:(NSTimer *)timer
{
if (ble.peripherals.count > 0)
{
[ble connectPeripheral:[ble.peripherals objectAtIndex:0]];
}
NSLog(#"connectionTimer"); // diag
}
// We call this when the view loads to try to connect to our bluetooth perepheral
//
- (void) scanForPeripherals
{
if (ble.activePeripheral)
if(ble.activePeripheral.state == CBPeripheralStateConnected)
{
statusLabel.text = #"Disconnectng from peripheral...";
[[ble CM] cancelPeripheralConnection:[ble activePeripheral]];
return;
}
if (ble.peripherals)
ble.peripherals = nil;
NSLog(#"scanning...");
statusLabel.text = #"Scanning for peripherals...";
[ble findBLEPeripherals:2];
[NSTimer scheduledTimerWithTimeInterval:(float)2.0 target:self selector:#selector(connectionTimer:) userInfo:nil repeats:NO];
//[indConnecting startAnimating];
}
- (IBAction)connect:(id)sender {
[self scanForPeripherals];
}
///////////////////////////////////////////////////////////
#pragma mark - BLE delegate
///////////////////////////////////////////////////////////
NSTimer *rssiTimer;
// When Connected, this will be called
-(void) bleDidConnect
{
NSLog(#"->Connected");
statusLabel.text = #"Connected!";
connectionStatus = #"Connected!";
// Schedule to read RSSI every 1 sec.
rssiTimer = [NSTimer scheduledTimerWithTimeInterval:(float)1.0 target:self selector:#selector(readRSSITimer:) userInfo:nil repeats:YES];
}
// When RSSI is changed, this will be called
-(void) bleDidUpdateRSSI:(NSNumber *) rssi
{
// Append the rssi value to our status label
//
NSString *temp = [NSString stringWithFormat:#"%# (%#)", connectionStatus, rssi];
statusLabel.text = temp;
}
-(void) readRSSITimer:(NSTimer *)timer
{
[ble readRSSI];
}
// When data is comming, this will be called
-(void) bleDidReceiveData:(unsigned char *)data length:(int)length
{
NSData *d = [NSData dataWithBytes:data length:length];
_myData = [NSData dataWithBytes:data length:length];
NSString *s = [[NSString alloc] initWithData:d encoding:NSUTF8StringEncoding];
NSLog(#"Datos en String %#",s);
//_datosdelegate = self;
//Is anyone listening
if([delegate respondsToSelector:#selector(actualizaDatos:)])
{
//send the delegate function with the amount entered by the user
[delegate actualizaDatos:_myData];
NSLog(#"Entro delegado");
}
}
- (void)bleDidDisconnect
{
NSLog(#"->Disconnected");
connectionStatus = #"Disconnected!";
statusLabel.text = #"Disconnected!";
[rssiTimer invalidate];
}
#end
sevenSegmentsViewController.h
#import <UIKit/UIKit.h>
#import "BLEViewController.h"
#import "BLE.h"
#interface sevenSegmentsViewController : UIViewController<EnviarDatos>{
UIImage *unoON;
UIImage *dosON;
UIImage *tresON;
UIImage *cuatroON;
UIImage *cincoON;
UIImage *seisON;
UIImage *sieteON;
UIImage *unoOFF;
UIImage *dosOFF;
UIImage *tresOFF;
UIImage *cuatroOFF;
UIImage *cincoOFF;
UIImage *seisOFF;
UIImage *sieteOFF;
}
#property (strong, nonatomic) IBOutlet UIImageView *uno;
#property (strong, nonatomic) IBOutlet UIImageView *dos;
#property (strong, nonatomic) IBOutlet UIImageView *tres;
#property (strong, nonatomic) IBOutlet UIImageView *cuatro;
#property (strong, nonatomic) IBOutlet UIImageView *cinco;
#property (strong, nonatomic) IBOutlet UIImageView *seis;
#property (strong, nonatomic) IBOutlet UIImageView *siete;
#end
sevenSegmentsViewController.m
//
// sevenSegmentsViewController.m
// iShield
//
// Created by Victor CarreƱo on 29/03/14.
// Copyright (c) 2014 RedBearLab. All rights reserved.
//
#import "sevenSegmentsViewController.h"
#interface sevenSegmentsViewController ()
#end
#implementation sevenSegmentsViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
unoOFF = [UIImage imageNamed:#"7segnh.png"];
dosOFF = [UIImage imageNamed:#"7segnv.png"];
tresOFF = [UIImage imageNamed:#"7segnv.png"];
cuatroOFF =[UIImage imageNamed:#"7segnh.png"];
cincoOFF = [UIImage imageNamed:#"7segnh.png"];
seisOFF = [UIImage imageNamed:#"7segnv.png"];
sieteOFF = [UIImage imageNamed:#"7segnh.png"];
unoON = [UIImage imageNamed:#"7segvh.png"];
dosON = [UIImage imageNamed:#"7segvv.png"];
tresON = [UIImage imageNamed:#"7segvv.png"];
cuatroON =[UIImage imageNamed:#"7segvh.png"];
cincoON = [UIImage imageNamed:#"7segvh.png"];
seisON = [UIImage imageNamed:#"7segvv.png"];
sieteON = [UIImage imageNamed:#"7segvh.png"];
_uno = [[UIImageView alloc]initWithImage:unoOFF];
_dos = [[UIImageView alloc]initWithImage:dosOFF];
_tres = [[UIImageView alloc]initWithImage:tresOFF];
_cuatro = [[UIImageView alloc]initWithImage:cuatroOFF];
_cinco = [[UIImageView alloc]initWithImage:cincoOFF];
_seis = [[UIImageView alloc]initWithImage:seisOFF];
_siete = [[UIImageView alloc]initWithImage:sieteOFF];
//BLEViewController *myBLE = [[BLEViewController alloc]init];
//BLE *myBLE = [BLEViewController theBLEObject];
//NSLog(#"%#", myBLE.myData);
BLEViewController *myBLE = [[BLEViewController alloc]init];
myBLE.delegate = self;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
#pragma mark Delegado de Actualizar datos
-(void) actualizaDatos :(NSData *)myData{
NSLog(#"Datos recividos");
NSLog(#"Imprimio mi data con exitos %#", myData);
}
#end
In a nutshell, your BLEViewController is setting its delegate property to "self" when I think you want it to get set to an instance of "sevenSegmentsViewController". As a result the "if([delegate respondsToSelector:..." test is failing and you are never hitting the call to actualizaDatos. If you are using protocols correctly, you don't really need to test for "respondsToSelector" because by definition, the delegate must support the protocol.
The compiler and IDE are not showing you the error because you declared the property of the BLEViewController as just type "id" instead of
id<EnviarDatos>
If you fix the property declaration to say that your delegate must support the right protocol, you'll immediately see the errors highlighted.