Open CV Tutorial on IOS not working, EXC_BAD_ACCESS - ios

I am trying to write a simple openCV app for iOS, but I am having trouble with using openCV functions in XCode. They will throw a EXC_BAD_ACCESS(code = 1), or not work entirely. I am using the processImage sample from the tutorial and that doesn't even work. I am using XCode 7.3 beta and believe I have installed openCV 2.4.11 correctly.
ViewController.h
#import <UIKit/UIKit.h>
#import <opencv2/highgui/cap_ios.h>
using namespace cv;
#interface ViewController : UIViewController <CvVideoCameraDelegate>
{
//IBOutlet UIImageView *imageView;
CvVideoCamera *camera;
}
#property (nonatomic, retain) CvVideoCamera* camera;
#end
ViewController.mm
#import "ViewController.h"
#import <opencv2/highgui/cap_ios.h>
#interface ViewController ()
#property (weak, nonatomic) IBOutlet UIImageView *imageView
#end
#implementation ViewController
#synthesize camera;
- (void)viewDidLoad {
[super viewDidLoad];
self.camera = [[CvVideoCamera alloc] initWithParentView: self.imageView];
self.camera.defaultAVCaptureDevicePosition = AVCaptureDevicePositionBack;
self.camera.defaultAVCaptureSessionPreset = AVCaptureSessionPresetHigh;
self.camera.defaultAVCaptureVideoOrientation = AVCaptureVideoOrientationPortrait;
self.camera.defaultFPS = 60;
self.camera.grayscaleMode = NO;
self.camera.delegate = self;
self.camera.useAVCaptureVideoPreviewLayer = YES;
self.imageView.frame = self.view.bounds;
[self.camera start];
}
#pragma mark - Protocol CvVideoCameraDelegate
#ifdef __cplusplus
- (void)processImage:(cv::Mat&)image;
{
// Do some OpenCV stuff with the image
Mat image_copy;
cvtColor(image, image_copy, CV_BGRA2BGR);
// invert image
bitwise_not(image_copy, image_copy); // ERROR IS THROWN HERE, BUT IT
// WILL ALSO THROW ON THE CVTCOLOR
// IF I REMOVE THIS LINE
cvtColor(image_copy, image, CV_BGR2BGRA);
}
#endif
-(void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#end

I don't see any problems in your code. Maybe try downloading the newest version of the framework from here. Note that the header file cap_ios.h has moved to videoio which means you'll have to import: Hope that helps
#import <opencv2/videoio/cap_ios.h>

Related

Use of undeclared identifier 'AIRGoogleMapOverlay' in AIRGoogleMapOverlayManager even after #import "AIRGoogleMapOverlay.h"

I am getting Use of undeclared identifier 'AIRGoogleMapOverlay' error when I am trying to run my react native project through Xcode. I am actually trying to open the app on my iPhone and thats when I encounter this error. If I simply say react-native run-ios, it builds and launches the app on simulator. I am trying to extract current device location which I can't do using a simulator.
The error occurs in the AirGoogleMapOverlayManager.m file which exists in the react-native-google-maps package.
AIRGoogleMapOverlay.h
#ifdef HAVE_GOOGLE_MAPS
#import <Foundation/Foundation.h>
#import <GoogleMaps/GoogleMaps.h>
#import <React/RCTBridge.h>
#import "AIRMapCoordinate.h"
#import "AIRGoogleMap.h"
#interface AIRGoogleMapOverlay : UIView
#property (nonatomic, strong) GMSGroundOverlay *overlay;
#property (nonatomic, copy) NSString *imageSrc;
#property (nonatomic, strong, readonly) UIImage *overlayImage;
#property (nonatomic, copy) NSArray *boundsRect;
#property (nonatomic, readonly) GMSCoordinateBounds *overlayBounds;
#property (nonatomic, weak) RCTBridge *bridge;
#end
#endif
AIRGoogleMapOverlay.m
#ifdef HAVE_GOOGLE_MAPS
#import "AIRGoogleMapOverlay.h"
#import <React/RCTEventDispatcher.h>
#import <React/RCTImageLoader.h>
#import <React/RCTUtils.h>
#import <React/UIView+React.h>
#interface AIRGoogleMapOverlay()
#property (nonatomic, strong, readwrite) UIImage *overlayImage;
#property (nonatomic, readwrite) GMSCoordinateBounds *overlayBounds;
#end
#implementation AIRGoogleMapOverlay {
RCTImageLoaderCancellationBlock _reloadImageCancellationBlock;
CLLocationCoordinate2D _southWest;
CLLocationCoordinate2D _northEast;
}
- (instancetype)init
{
if ((self = [super init])) {
_overlay = [[GMSGroundOverlay alloc] init];
}
return self;
}
- (void)setImageSrc:(NSString *)imageSrc
{
NSLog(#">>> SET IMAGESRC: %#", imageSrc);
_imageSrc = imageSrc;
if (_reloadImageCancellationBlock) {
_reloadImageCancellationBlock();
_reloadImageCancellationBlock = nil;
}
__weak typeof(self) weakSelf = self;
_reloadImageCancellationBlock = [_bridge.imageLoader loadImageWithURLRequest:[RCTConvert NSURLRequest:_imageSrc]
size:weakSelf.bounds.size
scale:RCTScreenScale()
clipped:YES
resizeMode:RCTResizeModeCenter
progressBlock:nil
partialLoadBlock:nil
completionBlock:^(NSError *error, UIImage *image) {
if (error) {
NSLog(#"%#", error);
}
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(#">>> IMAGE: %#", image);
weakSelf.overlayImage = image;
weakSelf.overlay.icon = image;
});
}];
}
- (void)setBoundsRect:(NSArray *)boundsRect
{
_boundsRect = boundsRect;
_southWest = CLLocationCoordinate2DMake([boundsRect[1][0] doubleValue], [boundsRect[0][1] doubleValue]);
_northEast = CLLocationCoordinate2DMake([boundsRect[0][0] doubleValue], [boundsRect[1][1] doubleValue]);
_overlayBounds = [[GMSCoordinateBounds alloc] initWithCoordinate:_southWest
coordinate:_northEast];
_overlay.bounds = _overlayBounds;
}
#end
#endif
And here is where I get the error, in AIRGoogleMapOverlayManager
#import "AIRGoogleMapOverlayManager.h"
#import "AIRGoogleMapOverlay.h"
#interface AIRGoogleMapOverlayManager()
#end
#implementation AIRGoogleMapOverlayManager
- (UIView *)view
{
AIRGoogleMapOverlay *overlay = [AIRGoogleMapOverlay new]; ERROR-Use of undeclared identifier 'AIRGoogleMApOverlay'
overlay.bridge = self.bridge; ERROR-Use of undeclared identifier 'overlay'
return overlay; ERROR-Use of undeclared identifier 'overlay'
return 0;
}
RCT_EXPORT_MODULE()
RCT_REMAP_VIEW_PROPERTY(bounds, boundsRect, NSArray)
RCT_REMAP_VIEW_PROPERTY(image, imageSrc, NSString)
#end
I know that solving the first will get rid of all 3 errors. Also I am running xcworkspace, not xcodeproj.
Any help is appreciated!
I added HAVE_GOOGLE_MAPS=1 to the preprocessor macros, all the AIRGoogleMapOverlay related errors were gone.
This needs to be added for both Debug and Release in case someone is wondering why they are getting this error while trying to create an offline bundle.
You might need a bridging header.
Create a file in your project, named for instance AIRGoogleMapOverlay-Bridging-Header.h
and put
#import "AIRGoogleMapOverlay.h"
in it.
Finally, add the header file in your Build settings like below, depending on the environment you need it to be set up.

How to change variable of custom UIView from ViewController

I have created a custom UIView (ProgressView) where I draw a shape imported via StyleKit from PaintCode.
Below are the codes. I have declared instance variable property in my custom UIView and when I try to change property from ViewController, It does not work.
ProgressView.h
#import <UIKit/UIKit.h>
#interface ProogressView : UIView
#property (nonatomic) float daysFraction;
#property (nonatomic) float pagesFraction;
#end
ProgressView.m
#import "ProgressView.h"
#import "StyleKitName.h"
#import "ViewController.h"
#implementation ProgressView
#synthesize daysFraction = _daysFraction;
#synthesize pagesFraction = _pagesFraction;
- (void)drawRect:(CGRect)rect {
// Drawing code
[StyleKitName drawCanvas1WithDaysFraction:self.daysFraction pageFraction:self.pagesFraction];
}
-(void)awakeFromNib {
[super awakeFromNib];
self.pagesFraction = 0;
self.daysFraction = 0;
}
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#end
*ViewController.m**
#import "ViewController.h"
#import "ButtonAnimation.h"
#import "ProgressView.h"
#import "StyleKitName.h"
#interface ViewController ()
#property (weak, nonatomic) IBOutlet ButtonAnimation *buttonView;
#property (weak, nonatomic) IBOutlet UIButton *actionButton;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
ProgressView *new =[[ ProgressView alloc]init];
new.daysFraction = 0.7f; // here I am setting value to variable in custom view ProgressView but not working.
}
- (IBAction)animateTheButton:(id)sender {
self.buttonView.layer.backgroundColor = [UIColor clearColor].CGColor;
[self.buttonView addErrorAnimation];
}
#end
You need to add this view to UIViewController's view:
[self.view addSubview:progressView];
Later, you must also set a frame. Eg
[progressView setFrame:self.view.bounds];
You may do it in viewDid/WillLayoutSubviews method to change it on rotation / window resize event.
BTW, do not name your view as new, it's horrible. Such name doesn't even tell what kind of variable is it.

Objective C Transparent UIViewController for Gameover Screen

Information: I would like to make a little Game (Not Important what kind of game) with a little Game Over Screen.
Because i cant Upload Images, i try to Describe it.
You Play, everything is alright. Then you get Gameover. The Screen should be the Same and an Overlayer is Showing the Score (Transparent / Alpha)
Now my First Question is: Is my Solution good?
I use one UIViewController from the Storyboard
Source:
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
{
IBOutlet UIButton *btnTest;
}
-(IBAction)btnTestClick;
#end
ViewController.m
#import "ViewController.h"
#import "MeinGottEh.h"
#interface ViewController ()
#end
#implementation ViewController
-(void)btnTestClick
{
self.modalPresentationStyle = UIModalPresentationCurrentContext;
MeinGottEh *sampleView = [[MeinGottEh alloc] init];
sampleView.labelText = #"High: 123";
[sampleView setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentModalViewController:sampleView animated:YES];
}
#end
Then the Second UIViewController (External File, not in Storyboard)
MeinGottEh.h
#import <UIKit/UIKit.h>
#interface MeinGottEh : UIViewController
{
IBOutlet UIButton *btnClose;
}
-(IBAction)btnCloseClick;
#property(nonatomic) NSString *labelText;
#end
MeinGottEh.m
#import "MeinGottEh.h"
#interface MeinGottEh ()
#end
#implementation MeinGottEh
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor clearColor];
[btnClose setTitle:self.labelText forState:UIControlStateNormal];
}
-(void)btnCloseClick
{
[self dismissModalViewControllerAnimated:YES];
}
#end
Is this Solution to Open a Highscore Box good? If no, please tell me how to do it (I make a Research on Stackoverflow. Thats the Most Parts)
If Yes: How can i make it Transparent?
Thank you very much.

How to implement a progress bar on top of UIWebView?

I am developing application for iOS7 in xcode 5. I have a view controller ADMSViewController
The .h file is as follows
#import <UIKit/UIKit.h>
#import "IMTWebView.h"
#interface ADMSViewController : UIViewController <UIWebViewDelegate,IMTWebViewProgressDelegate>
{
NSString *barcode;
NSString *vinNumber;
NSTimer *timer;
}
#property (nonatomic,retain) IBOutlet UIWebView *webView;
#property (nonatomic,retain) IBOutlet UIProgressView *progressView;
#end
The .m file is as follows
#import "ADMSViewController.h"
#import <QuartzCore/QuartzCore.h>
#implementation ADMSViewController
#synthesize webView;
#synthesize progressView;
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://google.com/"]]];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:YES animated:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[self.navigationController setNavigationBarHidden:NO animated:animated];
}
- (void)webView:(IMTWebView *)_webView didReceiveResourceNumber:(int)resourceNumber totalResources:(int)totalResources {
[self.progressView setProgress:((float)resourceNumber) / ((float)totalResources)];
if (resourceNumber == totalResources) {
_webView.resourceCount = 0;
_webView.resourceCompletedCount = 0;
}
}
- (void)viewDidUnload
{
[super viewDidUnload];
self.webView = nil;
self.progressView = nil;
}
#end
The IMTWebview.h file is as follows
#import <UIKit/UIKit.h>
#class IMTWebView;
#protocol IMTWebViewProgressDelegate <NSObject>
#required
- (void) webView:(IMTWebView*)webView didReceiveResourceNumber:(int)resourceNumber totalResources:(int)totalResources;
#end
#interface IMTWebView : UIWebView {
}
#property (nonatomic, assign) int resourceCount;
#property (nonatomic, assign) int resourceCompletedCount;
#property (nonatomic, assign) id<IMTWebViewProgressDelegate> progressDelegate;
#end
the IMTWebCiew.m file is as follows
#import "IMTWebView.h"
#interface UIWebView ()
-(id)webView:(id)view identifierForInitialRequest:(id)initialRequest fromDataSource:(id)dataSource;
-(void)webView:(id)view resource:(id)resource didFinishLoadingFromDataSource:(id)dataSource;
-(void)webView:(id)view resource:(id)resource didFailLoadingWithError:(id)error fromDataSource:(id)dataSource;
#end
#implementation IMTWebView
#synthesize progressDelegate;
#synthesize resourceCount;
#synthesize resourceCompletedCount;
-(id)webView:(id)view identifierForInitialRequest:(id)initialRequest fromDataSource:(id)dataSource
{
[super webView:view identifierForInitialRequest:initialRequest fromDataSource:dataSource];
return [NSNumber numberWithInt:resourceCount++];
}
- (void)webView:(id)view resource:(id)resource didFailLoadingWithError:(id)error fromDataSource:(id)dataSource {
[super webView:view resource:resource didFailLoadingWithError:error fromDataSource:dataSource];
resourceCompletedCount++;
if ([self.progressDelegate respondsToSelector:#selector(webView:didReceiveResourceNumber:totalResources:)]) {
[self.progressDelegate webView:self didReceiveResourceNumber:resourceCompletedCount totalResources:resourceCount];
}
}
-(void)webView:(id)view resource:(id)resource didFinishLoadingFromDataSource:(id)dataSource
{
[super webView:view resource:resource didFinishLoadingFromDataSource:dataSource];
resourceCompletedCount++;
if ([self.progressDelegate respondsToSelector:#selector(webView:didReceiveResourceNumber:totalResources:)]) {
[self.progressDelegate webView:self didReceiveResourceNumber:resourceCompletedCount totalResources:resourceCount];
}
}
#end
My issue is that the progress bar is not animating in the webview. Please help me as I am a newbie to ios development. If there is any other method to implement the same, do inform me.
Thank you in advance for your answers.
For this kind of thing, you can only really 'detect' page size by using Content-Length headers, but otherwise you can still present a loading control (like one of the moving zebra crossing type bars which doesn't show a discrete % progress but shows that activity is occurring.. or even just a UIActivityIndicator). You could try a ready-made class like http://allseeing-i.com/ASIHTTPRequest/ASIWebPageRequest too, which CAN show progress. The author states:
ASIWebPageRequests do not currently support progress tracking for an entire request with its external resources. However, progress updates from external resource requests are passed on to your delegate, so with a bit of work it may be possible to implement this yourself.

CALayers and Audio

I have a CALayer with an image and I have that animates when I touch it, it works, it's great. What I want to do now is also have a sound clip that plays when you press the same image. I'm finding this tricky because the CALayer is from the UIView Class called BounceView. I created an instance of BounceView on my MainViewController. I apologize if I didn't use correct terminology. Do I need to place my audio code in my MainViewController and then use delegation to allow the BounceView to trigger a method on the MainViewController? Any help or direction is greatly appreciated.
MainViewController.h
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <QuartzCore/QuartzCore.h>
#import <AVFoundation/AVFoundation.h>
#import <CoreAudio/CoreAudioTypes.h>
#interface MainViewController : UIViewController <AVAudioPlayerDelegate>
#property (nonatomic, retain) IBOutlet UIView *BounceView;
#property (strong, nonatomic) AVAudioPlayer *audioPlayer;
- (IBAction)playAudio:(id)sender;
#end
MainViewController.m
#import "MainViewController.h"
#import "BounceView.h"
#interface MainViewController ()
#end
#implementation MainViewController
#synthesize BounceView;
#synthesize audioPlayer;
- (void)viewDidLoad
{
[super viewDidLoad];
//Setup the audio player
NSURL *noSoundFileURL=[NSURL fileURLWithPath:
[[NSBundle mainBundle]
pathForResource:#"InTheMood" ofType:#"mp3"]];
self.audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:noSoundFileURL error:nil];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (void)loadView
{
NSLog(#"loadView");
// Create a view
CGRect frame = [[UIScreen mainScreen] bounds];
BounceView *v = [[BounceView alloc] initWithFrame:frame];
// Set it as *the* view of this view controller
[self setView:v];
}
- (IBAction)playAudio:(id)sender {
// self.audioPlayer.delegate=self;
[self.audioPlayer play];
}
#end
BounceView.h
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <QuartzCore/QuartzCore.h>
#import "MainViewController.h"
#interface BounceView : UIView
{
CALayer *myLayer;
}
#end
BounceView.m
I will provide this code if requested but I don't want to overload code. Here I create new layer open (alloc init), give it size, position, create UIImage, get underlying CGImage & put it on the layer then make sublayer of the view's layer.
Again, any help is greatly appreciated.
Since BounceView is an UIView class not a UIViewController, I would let the playing audio to the MainViewController.

Resources