How Do I Change The Frame Of A ZBar Reader? - iphone

Ok, so I'm using the ZBar SDK to scan barcodes in my iPhone app. I've successfully implemented the sample code, but now I want to change the frame of the scanner view (i.e: To half the screen size). I've tried setting the frame of the reader's view in viewDidLoad, but it resizes itself.
I know this is going to be one of those really simple things I just missed, but any help would be much appreciated. Cheers.
EDIT: I got it to work. Here's my code:
ZBarReaderViewController *reader = [ZBarReaderViewController new];
reader.readerDelegate = self;
ZBarImageScanner *scanner = reader.scanner;
[reader setShowsZBarControls:NO];
[reader.readerView setScanCrop:(CGRect){ { 0, 0 }, { 0.43, 1 } }];
[reader.readerView start];
[self.view addSubview:reader.view];
overlayView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
[listTableView setFrame:CGRectMake(0, 208, 320, 208)];
[overlayView addSubview:listTableView];
[self.view addSubview:overlayView];

Instead of using ZBarReaderViewController, try using ZBarReaderView. This worked for me and saved my lot of time. Hope it helps you.
ZBarReaderView*reader = [ZBarReaderView new];
ZBarImageScanner * scanner = [ZBarImageScanner new];
[scanner setSymbology: ZBAR_I25
config: ZBAR_CFG_ENABLE
to: 0];
reader = [reader initWithImageScanner:scanner];
reader.readerDelegate = self;
reader.tracksSymbols = YES;
reader.frame = CGRectMake(20, 38, 283, 347);
reader.torchMode = 0;
dispatch_async(dispatch_get_main_queue(), ^{[reader start];});
[self.view addSubview:reader];

I worked it out. This is what I had to do:
Add ZBarReaderViewController's view as subview of my own view (which annoyingly fills the entire view no matter what its frame is).
Change scan size of ZBarReaderViewController to whatever size I want it to be (BEWARE: Setting this frame is not like setting one normally, just ask if you need help).
Add any views that you want visible to the ZBarReaderViewController's overlay view.
This was very difficult and unintuitive and broke many of Apple's code design guidelines but, in the end, is still doable.

You can create your own view and view controller,and add the ZBarReaderViewController's view as a subview of your own view;

another way to modify the properties of the scanning view controller is to import the ZBarSDK project and compile and link it yourself, rather than using the binary version of the SDK. Then, you can make any changes to the view controller that you need to (keep in mind their license...should probably read that first)

Try this
It may help you:
ZBarReaderViewController *reader= [ZBarReaderViewController new];
reader.readerDelegate = self;
reader.supportedOrientationsMask = ZBarOrientationMaskAll;
ZBarImageScanner *scanner = reader.scanner;
// TODO: (optional) additional reader configuration here
// reader.showsCameraControls = NO; // for UIImagePickerController
reader.showsZBarControls = NO;
// EXAMPLE: disable rarely used I2/5 to improve performance
[scanner setSymbology:ZBAR_I25|ZBAR_QRCODE
config: ZBAR_CFG_ENABLE
to: 0];
[reader viewDidLoad];
[reader viewWillAppear:NO];
[reader viewDidAppear:NO];
[self.viewScan addSubview:reader.view];
here,self.viewScan is any view of your current controller.
so scanning area now self.viewScan view.

The best way to do it so it will be inside a sampleView :
UIView *view = [self sampleView];
CALayer *viewLayer = [view layer];
[viewLayer setMasksToBounds:YES];
CGRect bounds = [view bounds];
[reader.view.layer setFrame:bounds];
[viewLayer insertSublayer:reader.view.layer below:[[viewLayer sublayers] objectAtIndex:0]];

Related

How to open camera in small region of view in ios

I have to open camera in my app in small region of screen ,but not getting how to implement it,because camera opens on full screen genreally.
The camera should be open in uiimage view region,Plz guide me how to achieve this.Thanks.
You should take a look at AVCam sample project provided by Apple. It has all the features you need for your task. Good Luck!
I experimented with the code from my last post, and commented out the final scale transform ((the one which makes it full size) and I ended up with a lovely miniature camera imagePicker floating in the middle of my screen, so it definitely does work! The exact code I used, including the zoom/fade-in transition, is -
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.delegate = self;
imagePickerController.mediaTypes = [NSArray arrayWithObjects:(NSString *)kUTTypeImage, nil];
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePickerController.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
UIView *controllerView = imagePickerController.view;
controllerView.alpha = 0.0;
controllerView.transform = CGAffineTransformMakeScale(0.5, 0.5);
[[[[UIApplication sharedApplication] delegate] window] addSubview:controllerView];
[UIView animateWithDuration:0.3
delay:0.0
options:UIViewAnimationOptionCurveLinear
animations:^{
controllerView.alpha = 1.0;
}
completion:nil
];
[imagePickerController release];
you can go through my answer here.

iOS - UIScrollView is not working (it doesn't scroll at all - the image stays fixed)

I would like to display an image (width: 320 pixels, height: 1250 pixels) in an image view.
When I compile the application I get no scrolling at all. The view stays fixed.
What I did:
Added an UIScrollView via Interface Builder to my view.
Added an UIImageView via Interface Builder to my view.
Verified that UIImageView is below UIScrollView in Interface Builder.
Set the size of the UIScrollView with ViewDidLoad.
How do I do this?
Code:
- (void)viewDidLoad
{
[super viewDidLoad];
scrollView.contentSize = CGSizeMake(320, 1250);
}
Screenshots:
ImageView:
ScrollView:
I just have done the same task..
Try this one.....
scrollView.delegate = self;
scrollView.scrollEnabled = YES;
int scrollWidth = 120;
scrollView.contentSize = CGSizeMake(scrollWidth,80);
int xOffset = 0;
imageView.image = [UIImage imageNamed:[imagesName objectAtIndex:0]];
for(int index=0; index < [imagesName count]; index++)
{
UIImageView *img = [[UIImageView alloc] init];
img.bounds = CGRectMake(10, 10, 50, 50);
img.frame = CGRectMake(5+xOffset, 0, 50, 50);
NSLog(#"image: %#",[imagesName objectAtIndex:index]);
img.image = [UIImage imageNamed:[imagesName objectAtIndex:index]];
[images insertObject:img atIndex:index];
scrollView.contentSize = CGSizeMake(scrollWidth+xOffset,110);
[scrollView addSubview:[images objectAtIndex:index]];
xOffset += 70;
}
Also set this one....
imagesName = [[NSArray alloc]initWithObjects:#"image1.jpg",#"image2.jpg",#"image3.jpg",#"image4.jpg",#"image5.jpg",#"image6.png",#"image7.png",#"image9.png",nil];
images = [[NSMutableArray alloc]init];
So for me the problem was that setting the content size didn't work in viewDidLoad(). I tried everything and I didn't understand why it wouldn't want to work, and then I tried the same stuff in viewDidAppear() and it magically worked...
From you last screenshot and from your comments it looks like your scrollView is way to big.
The scrollview must be visible on screen completely. For example a full screen UIScrollView on iPhone would have a size of 320 x 460.
If the scrollview is the same size as its content you can't scroll.
The greenish rectangle shows the size of your scrollview, the pinkish the size of your content (your image):
Xcode 11+, Swift 5
You can find the complete solution here.
I came across this same issue on iOS6 and the solution was to programmatically adjust the ContentSize.
So I will just quote from Raja (above) to show this:
CGSize scrollViewContentSize = CGSizeMake(320, 400);
[self.scrollView setContentSize:scrollViewContentSize];
NOTE: I was not having this issue on iOS5.. seems like iOS6 decided to do alot of prank just like the rotation/orientation saga
Since Xcode 5 it does not work like before. Also scrolling to the end of a scrollable text field makes problems. There are also differences between doing it on iPhone or iPad. On iPhone it worked without delayed timer.
This worked for me:
- (void)viewDidLoad
{
[super viewDidLoad];
NSTimer *timerforScrollView;
timerforScrollView =[NSTimer scheduledTimerWithTimeInterval:0.1
target:self selector:#selector(forScrollView)userInfo:nil repeats:NO];
}
- (void) forScrollView {
[scrollviewPad setScrollEnabled:YES];
[scrollviewPad setContentSize:CGSizeMake(768, 1015)]; // must be greater then the size in Storyboard
}
I found I had a similar problem but none of the above code worked. The issue was due to autolayout. I found that if I turned off autolayout by going to the storyboard clicking on Utilities -> File Inspector and unchecked Use Autolayout the scrolling did work (I also set scroll.contentSize = ...).
Sometimes autoLayout checkbox is ticked in the xib. That also creates this issue in xcode 5. Which makes the UIScrollView scrolling off.
Don't forget to add the category protocol to the interface, like this
#interface MyViewController : <UIScrollViewDelegate>
If you don't, you will not be able to set the scroll view delegate to self
(i.e. [MyScrollView setDelegate:self];)
If you do this, it should work.
My code is:
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[contentScrollView setDelegate:self];
[contentScrollView setScrollEnabled:YES];
contentScrollView.contentSize = CGSizeMake(310, 500);
contentScrollView.frame = CGRectMake(5, 188, 310, 193);
}
Did you assign the scroll's view delegate? Always remember these:
[self.scrollView setDelegate:self];
[self.scrollView setScrollEnabled:YES];
The image view has to be a subview (so inside AND below) of the scrollview. From your description it seems they are paralell
You forgot one line. Add this to your view load function:
[scrollView setScrollEnabled:YES];
You could try disabling AutoLayout. In XCode 5 I tested all the above answers and I could only scroll it by disabling autolayout and activating all autosizing masks under the Size Inspector. The following code was used too:
self.scrollView.contentSize = CGSizeMake(320, 900);
self.scrollView.delegate = self;
self.scrollView.scrollEnabled = YES;
self.scrollView.frame = self.view.frame;
CGRect scrollViewFrame = CGRectMake(0, 0, 320, 400);
self.scrollView = [[UIScrollView alloc] initWithFrame:scrollViewFrame];
[self.view addSubview:self.scrollView];
CGSize scrollViewContentSize = CGSizeMake(320, 400);
[self.scrollView setContentSize:scrollViewContentSize];
scrollView.delegate = self;
[self.scrollView setBackgroundColor:[UIColor blackColor]];
[scrollView setCanCancelContentTouches:NO];
scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
scrollView.clipsToBounds = YES;
scrollView.scrollEnabled = YES;
scrollView.pagingEnabled = YES;
NSUInteger nimages = 0;
CGFloat cx = 0;
for (; ; nimages++) {
NSString *imageName = [NSString stringWithFormat:#"image%d.jpg", (nimages + 1)];
UIImage *image = [UIImage imageNamed:imageName];
if (image == nil) {
break;
}
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
CGRect rect = imageView.frame;
rect.size.height = image.size.height;
rect.size.width = image.size.width;
rect.origin.x = ((scrollView.frame.size.width - image.size.width) / 2) + cx;
rect.origin.y = ((scrollView.frame.size.height - image.size.height) / 2);
imageView.frame = rect;
[scrollView addSubview:imageView];
[imageView release];
cx += scrollView.frame.size.width;
}
[scrollView setContentSize:CGSizeMake(cx, [scrollView bounds].size.height)];
Assuming that scrollView is a subview of view and fills it entirely you can use the following in viewDidLoad:
[scrollView setContentSize: CGSizeMake(self.view.frame.size.width, self.view.frame.size.height)];
I had a UIScrollView that was not scrolling and this allowed it to scroll.
Just add code
scrollView.contentSize = CGSizeMake(WIDTH,HEIGHT);
to method -(void)viewDidLayoutSubviews.
For more information checkout Stanford CS193p Lecture No 8 to understand View Controller Life cycle.
I had the same issue and was looking for the answer in this thread. I tried all the stuff, but nothing works. Then I found this:
.
You just need to deselect "Use Auto Layout" in File Inspector of your ViewController. Ta-Da, it works immediately for me. Enjoy.

Adding activityindicator on Default image - code given

I have an imaged name Default.png. This will load when the application is launching. Now i need to add an activity indicator to it. So it will spin and make the app look nice.
We know that we can't add any UI Components when the app is loading. So i thought to add the UIActivityIndicator in the didFinishLaunchingWithOptions method in the App delegate.
These are the steps i followed.
i added a view
added the default.png
added activity indicator
then
[window addSubView:view];
But nothing hapence.
Help how to write the code for this ?
You're on the right track, but you do need to add another UIView to your window (not another UIViewContoller, necessarily) temporarily. I do this sort of thing often. Here's some code that would be appropriate for your applicationDidFinishLaunching:withOptions: method:
[window makeKeyAndVisible]
...
// Create and show an overlay view with a spinner
UIImage *defaultImage = [UIImage imageNamed:#"Default.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:defaultImage];
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
// Position the spinner appropriately for our splash image so it's not obscured
CGRect frame = spinner.frame;
frame.origin.x = imageView.frame.size.width / 2 - frame.size.width / 2;
frame.origin.y = imageView.frame.size.height / 5 * 4 - frame.size.height / 2;
spinner.frame = frame;
[spinner startAnimating];
[spinner setHidesWhenStopped:YES];
startupView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
[startupView addSubview:imageView];
[imageView release];
[startupView addSubview:spinner];
[spinner release];
[window addSubview:startupView];
The ivar startupView belongs to the app delegate, and later in the startup sequence another method removes it gracefully by fading it from view:
UIActivityIndicatorView *spinner = [[startupView subviews] lastObject];
[spinner stopAnimating];
[UIView animateWithDuration:1.0
delay:0
options:UIViewAnimationOptionCurveLinear
animations:^(void) {
startupView.alpha = 0.0;
} completion:^(BOOL finished) {
[startupView removeFromSuperview];
[startupView release];
}];
It's worth mentioning that splash screens like this are not the "recommended" startup screen in Apple's view. But they do not seem to reject apps that have them.
I think that you wanna do a "custom splash screen" isnt it?
You cannot add the activity indicator in this way!
You need add a view controller, and at this view controller do stuff that you need with activity indicator!

Image not appears in the view (iPhone)

I have a viewcontroller which can't display the image,here is my code:
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor blackColor];
dispatch_queue_t downloadQueue =
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(downloadQueue, ^{
NSData *mydata = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageUrl]];
image = [UIImage imageWithData:mydata];
if (image) {
[imageView initWithImage:image];
scrollView.contentSize = image.size;
scrollView.bounces = NO;
scrollView.minimumZoomScale = 0.5;
scrollView.maximumZoomScale = 2.0;
scrollView.delegate = self;
[scrollView addSubview:imageView];
[self.view addSubview:scrollView];
}
});
dispatch_release(downloadQueue);
}
It worked very well previously,but after I have made some changes in other viewcontrollers(I didn't make any changes in this one), it didn't show the image anymore, I can't figure out what's going on.
when I use "dismissModalViewControllerAnimated" function to dismiss this viewcontroller , I did see the image at that moment; and when I make the app background and put it front again, the image appears magically.
I 'am totally confused! any suggestion?
my problem got solved by putting these code in the main quene, thank you for all your attention.
dispatch_async(dispatch_get_main_queue(), ^{
image = [UIImage imageWithData:mydata];
if (image) {
[imageView initWithImage:image];
scrollView.contentSize = image.size;
scrollView.bounces = NO;
scrollView.minimumZoomScale = 0.5;
scrollView.maximumZoomScale = 2.0;
scrollView.delegate = self;
[scrollView addSubview:imageView];
[self.view addSubview:scrollView];
}
});
you can use [scrollView bringSubviewToFront:imageView];
and also be sure that your imageView is already allocated some memory.
Your problem has to be somewhere else, or a problem with the image itself. It wouldn't have caused a problem here unless you changed it somewhere else in your code. From what I can tell, all the code here is completely correct. Check in the other view controllers you added and also in the XIBs to see if everything is connected correctly. Even if it is, reconnect the image view and scroll view. Is the scroll view displaying correctly?
can you debug and check what is the size of scrollView after this:
scrollView.contentSize = image.size;
else try keeping it static and try, if it shows the result then its problem with your content size as you keeping it image.size.
then type
scrollView.contentSize = CGSizeMake(image.frame.size.width, image.frame.size.height);
it will probably work.
Please check out the frame of image view. if its (0,0) it will not show the result.
and replace image with image view in below code.
scrollView.contentSize = CGSizeMake(imageView.frame.size.width, imageView.frame.size.height);

Mirror iPhone on external display with cocos2d

I want to reproduce my iPhone display for demoing purposes. I've tried
this and this, but both give me a black screen. Finally, I headed out to make my own solution. Here's what I have:
// Check for external screen.
if ([[UIScreen screens] count] > 1) {
externalWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Internal display is 0, external is 1.
externalScreen = [[[UIScreen screens] objectAtIndex:1] retain];
screenModes = [externalScreen.availableModes retain];
UIScreenMode *desiredMode = [screenModes objectAtIndex:0];
externalScreen.currentMode = desiredMode;
externalWindow.screen = externalScreen;
[screenModes release];
[externalScreen release];
CGRect rect = CGRectZero;
rect.size = desiredMode.size;
externalWindow.frame = rect;
externalWindow.clipsToBounds = YES;
externalWindow.hidden = NO;
[externalWindow makeKeyAndVisible];
[externalWindow setUserInteractionEnabled:YES];
[externalWindow setMultipleTouchEnabled:YES];
//[[CCDirector sharedDirector] attachInView:externalWindow];
Now, I can display on the external display or on my iPhone, but I can't display on both at the same time, because [[CCDirector sharedDirector] attachInView:externalWindow]; will only take one UIWindow. How can I get around this and/or get the displayed image and set it to my external display?
Thanks,
Dave
The only solution i see is to render your scene to a texture and render that texture twice as a full screen quad to the backbuffer, once for each UIWindow