I'm building a camera application, and I can't get the zooming on the image to work. I honestly don't know where to go from here, I'm sure it's some dumb mistake that I'm missing though. The app sets the chosenImageView to the workingImage (picture from the camera). When I try to zoom, nothing happens.
ViewController.h
#interface ViewController : UIViewController <UIActionSheetDelegate, UIImagePickerControllerDelegate, UIScrollViewDelegate>
#property (strong, nonatomic) UIImage *workingImage;
#property (weak, nonatomic) IBOutlet UIImageView *chosenImageView;
#property (weak, nonatomic) IBOutlet UIScrollView *imageScroller;
-(IBAction)cameraButtonPressed;
#end
ViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.imageScroller.minimumZoomScale = 0.01f;
self.imageScroller.maximumZoomScale = 6.0f;
self.imageScroller.contentSize = self.imageScroller.frame.size;
self.imageScroller.scrollEnabled = YES;
self.navigationController.navigationBar.tintColor = [UIColor blackColor];
}
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return self.chosenImageView;
}
Here was the problem. You have to control+drag from the UIScrollView to the View Controller to set the delegate.
Related
I'm trying to create an app that has three UISlider used for selecting R, G and B values. The slider should change the colour of a label according to the sliders values.
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
{
NSInteger color;
IBOutlet UISlider *redSlider;
IBOutlet UISlider *greenSlider;
IBOutlet UISlider *blueSlider;
IBOutlet UILabel *colorLabel;
}
#property (nonatomic, retain) UISlider *redSlider;
#property (nonatomic, retain) UISlider *greenSlider;
#property (nonatomic, retain) UISlider *blueSlider;
colorLabel.textColor = [UIColor colorWithRed: redSlider.value green:greenSlider.value
blue:blueSlider.value alpha:1];
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#synthesize redSlider, greenSlider, blueSlider;
#end
This is what I have so far and I'm totally stuck.
You can do this like...
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
// you need to make sure these are actually created
// and give them all a target of self and action sliderValueChanged
#property (nonatomic, retain) UISlider *redSlider;
#property (nonatomic, retain) UISlider *greenSlider;
#property (nonatomic, retain) UISlider *blueSlider;
// not sure where this is set up
#property (nonatomic, strong) UILabel *colorLabel;
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// You may have to create your labels and sliders in here...
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)sliderValueChanged
{
// the sliders have changed so set the colour of the label
// based on the new values of the sliders.
self.colorLabel.textColor = [UIColor colorWithRed:self.redSlider.value
green:self.greenSlider.value
blue:self.blueSlider.value
alpha:1.0];
}
#end
Because you don't actually have a question I'm going to take a guess that your code doesn't compile, because it wouldn't.
colorLabel.textColor = [UIColor colorWithRed: redSlider.value green:greenSlider.value blue:blueSlider.value alpha:1];
The above line doesn't belong in you interface. I would recommend you change the code you currently have to something like.
ViewController.h
#interface ViewController : UIViewController
// You don't need the ivars anymore these get generated with the properties below
#property (nonatomic, retain) IBOutlet UISlider *redSlider;
#property (nonatomic, retain) IBOutlet UISlider *greenSlider;
#property (nonatomic, retain) IBOutlet UISlider *blueSlider;
#property (nonatomic, retain) IBOutlet UILabel *colorLabel
#property (assign) NSInteger color;
#end
ViewController.m
#implementation ViewController
// Also no need for the synthesize as these also get generated automatically
- (void)viewDidLoad
{
[super viewDidLoad];
// To change the actual color when changing the value of a slider move this to a method that gets called on slider changed.
colorLabel.textColor = [UIColor colorWithRed:self.redSlider.value green:self.greenSlider.value self.blue:blueSlider.value alpha:1];
}
#end
And once you actually ask a question I might also be able to help you with that.
Your ViewController.h :-
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
{
IBOutlet UISlider *redSlider;
IBOutlet UISlider *greenSlider;
IBOutlet UISlider *blueSlider;
IBOutlet UILabel *colorLabel;
}
#property (nonatomic, retain) UISlider *redSlider;
#property (nonatomic, retain) UISlider *greenSlider;
#property (nonatomic, retain) UISlider *blueSlider;
#end
and your ViewController.m will be like this:-
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize redSlider, greenSlider, blueSlider;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self changeLabelColor];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(Void) changeLabelColor
{
colorLabel.backgroundcolor = [UIColor colorWithRed:redSlider.value green:redSlider.value blue:redSlider.value alpha:1.0];
}
#end
As i didn’t copied paste and i have written it, there might be some typo..
textcolor wouldn’t change label color so i changed it to backgroundcolor.
you can also add slider method with IBAction.
I've tried searching for answers for this, but I have no idea what to search for and I'm at wit's end. I've got a UIScrollView that, when zooming, completely flips out and the image glitches everywhere before disappearing entirely. It even manages to, on occasion, temporarily screw up other components that aren't even part of the UIScrollView in which the zooming is happening. I apologize I can't be more technical. I wouldn't even know which part of my code to look at.
I made a video, because I have no technical terminology to describe what's happening here. Any help would be appreciated.
https://www.youtube.com/watch?v=SvhqHI-3_g8
The red is my current background of the UIScrollView. Don't mind that.
ViewController.h
#interface ViewController : UIViewController <UIActionSheetDelegate, UIImagePickerControllerDelegate, UIScrollViewDelegate>
#property (strong, nonatomic) UIImage *workingImage;
#property (weak, nonatomic) IBOutlet UIImageView *chosenImageView;
#property (weak, nonatomic) IBOutlet UIScrollView *imageScroller;
-(IBAction)cameraButtonPressed;
#end
ViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.imageScroller.minimumZoomScale = 1.00f;
self.imageScroller.maximumZoomScale = 15.0f;
self.imageScroller.contentSize = self.imageScroller.frame.size;
self.imageScroller.scrollEnabled = YES;
}
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return self.chosenImageView;
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *chosenImage = [info objectForKey:UIImagePickerControllerOriginalImage];
[self.chosenImageView setImage:chosenImage];
[self.imageScroller setContentSize:chosenImage.size];
[self dismissViewControllerAnimated:YES completion:nil];
}
See if this article helps. It seems the missing ScrollViewDidZoom method might be key to maintaining the zoomed view in place. http://www.raywenderlich.com/10518/how-to-use-uiscrollview-to-scroll-and-zoom-content
So I have a ZBarView within a UIViewController (for an embedded scanner) and I was wondering how I could change the Green Border to have it something like this?
#import "ZBarSDK.h"
#interface ScanViewController : UIViewController <ZBarReaderViewDelegate>
#property (nonatomic, strong) IBOutlet ZBarReaderView *readerView;
#property (strong, nonatomic) IBOutlet UILabel *scannedMachineLabel;
#end
- (void)viewDidLoad
{
[super viewDidLoad];
_readerView.readerDelegate = self;
// ensure initial camera orientation is correctly set
UIApplication *app = [UIApplication sharedApplication];
[_readerView willRotateToInterfaceOrientation: app.statusBarOrientation
duration: 0];
}
I have view controller, and inside that I have a UIScrollView, for managing the zoom, and a UIImage.
Now I'm handling the zoom effect with UIScrollViewDelegate delegate and method viewForZoomingInScrollView:... but the result is very poor, definitely not fluid!
This is my code:
#import "ImageViewController.h"
#interface ImageViewController () <UIScrollViewDelegate, UIActionSheetDelegate>
#property (strong, nonatomic) IBOutlet UIScrollView *scrollView;
#property (weak, nonatomic) IBOutlet UIImageView *imageView;
#end
#implementation ImageViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.scrollView.delegate = self;
self.scrollView.maximumZoomScale = 3.0;
// some code
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return self.imageView;
}
// last update
- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
if ([self.scrollView zoomScale] < 1.0) {
[self.scrollView setZoomScale:1.0];
}
}
// some other methods...
#end
This is the storyboard view:
I'm not using a Pinch Gesture Recognizer.
All images are quite small like 640x480.
Thanks
P.S. this is a continue of UICollectionView and images.
Checkout out this tutorial I wrote. The first part is exactly what you need to implement.
This has been addressed in other posts:
Zooming image views with autolayout:
Centering logic for autolayout or springs and struts
I've tried writing this code but I don't think it's correct cause there is an error with the viewDidLoad.
I'm trying to switch between 2 images in the same UIImageView using UISegmentcontol using Storyboard. All the examples online are for switching labels and not using Xcode storyboard.
h.file
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
{
IBOutlet UIImageView *myImage;
IBOutlet UISegmentedControl *segControl;
}
#property(nonatomic, retain)IBOutlet UIImageView *myImage;
#property (nonatomic, retain)IBOutlet UISegmentedControl *segControl;
- (IBAction)switchButton:(id)sender;
#end
m.file
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize myImage;
#synthesize segControl;
- (IBAction)switchButton:(id)sender
{
if (segControl.selectedSegmentIndex == 0)
{
[myImage setImage:[UIImage imageNamed:#"image1.jpg"]];
}
else
if (segControl.selectedSegmentIndex == 1)
{
[myImage setImage:[UIImage imageNamed:#"image2.jpg"]];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIImage *myImage = [myImage setImage:[UIImage imageNamed:#"image1.jpg"]];
}
If you want to fill the segments of the segmented control with images, you don't need code. Just use the storyboard.