Day 61 – Make my first iPhone app in 16 weeks (More C++)

This is part of “Make my first iPhone app in 16 weeks” series.

#include 
#include 
#include 

class MyClass
{
public:
    int n1;
    int n2;
};

void myFuncByPointer(MyClass *mc);
void myFuncByValue(MyClass mc);
void myFuncByReference(MyClass &mc);

int main(int argc, const char * argv[])
{
    // Create a new instance of MyClass
    MyClass mc = MyClass();

    myFuncByPointer(&mc);
    std::cout << "The values are " << mc.n1 << " and " << mc.n2 << "\n";

    myFuncByValue(mc);
    std::cout << "The values are " << mc.n1 << " and " << mc.n2 << "\n";

    myFuncByReference(mc);
    std::cout << "The values are " << mc.n1 << " and " << mc.n2 << "\n";               
    return 0; 
} 

// Retains value and faster as only passing in 4-byte pointer
void myFuncByPointer(MyClass *mc) 
{     
    mc -> n1 = 3;
    mc -> n2 = 5;
}

// Changes only live in the function and slower as passing the entire object
void myFuncByValue(MyClass mc)
{
    mc.n1 = 6;
    mc.n2 = 9;
}

// Retains value and faster as C++ automatically changes the argument to a pointer which is only 4-byte as opposed to passing the entire object.
void myFuncByReference(MyClass &mc)
{
    mc.n1 = 9;
    mc.n2 = 2;
}

The results are

The values are 3 and 5
The values are 3 and 5
The values are 9 and 2
Program ended with exit code: 0

Day 60 – Make my first iPhone app in 16 weeks (C++)

This is part of “Make my first iPhone app in 16 weeks” series.

Cocoa Touch (iOS) provides a library full of objects for your use.

@class vs. #import vs. #include

  • @class – is used when you need to know the name of a class, but don’t need to use it yet. Used in the header file. @class does not create dependency. If file B has @class file A, even if something changes in file A, file B will not be recompiled.
  • #import – is used when you actually need to use the class. Used in both header and implementation files. If file B has #import file A, file B is dependent on file A. If anything changes in file A, file B needs to be recompiled. If imported multiple times, it will only be imported once.
  • #include – same as #import but will be loaded multiple times if included several times. Just use #import.

Value vs. Reference(&) and Pointer(*) Recap

  • int *Pointer = &Integer
    • Pointer is a pointer to Integer (&Integer)
    • *Pointer is Integer
    • &Integer is a pointer to Integer
  • Visualization (PO box is a memory space)
    • There is a rose in your PO box – Value
    • The address for your PO box is “3847567” – Reference
    • There is a PO box that has a piece of paper that reads “3847567” – Pointer
  • Meaning
    • *Pointer (Go to PO box “3847567” and get the thing inside) == Value (A rose)
    • Pointer (Get the thing inside this pointer PO box) == &Value (Get the address of the PO box that holds this rose)

In C++, you can pass the pointer to an object into a function instead of the object itself.

int main(){ 
Class1 obj; 
doSomething(obj); 
};
void doSomething (Class1& obj) 
{ 
obj.x = 1; 
};
int main(){ 
Class1 obj; 
obj = doSomething(obj); 
};
Class1 doSomething (Class1 obj) 
{ 
obj.x = 1; 
return obj; 
};

Class Instantiation

  • Dog *HenryTheDog = new Dog(‘Henry’) creates a pointer to the new Dog named ‘Henry’
  • Dog HenryTheDog = Dog(‘Henry’) creates a new Dog named ‘Henry’

Day 58 to 59 – Make my first iPhone app in 16 weeks (OpenCV & C++)

This is part of “Make my first iPhone app in 16 weeks” series.

In order to do edge detection and image distortion, I can use an open-source library called OpenCV (CV stands for computer vision). I have had so much fun trying to install and built OpenCV framework but finally got some help and finished installing it.

Remember installation of OpenCV involves multiple languages. The library itself is written in C++, the build program that creates OpenCV framework is in Python and you are to use the framework in Objective-C++ file (change your view controller’s extension from .m to .mm).

If you follow the instruction in this link to the t, you will be able to install it without problem. Except I had a minor problem which I could not detect until I got someone else to give me a helping hand. The folder I cloned git repository to read “OpenCV iOS Project” with two spaces in the folder name. Darn!!!!! The build program did NOT like it! When I deleted the spaces and changed the folder name to “OpenCViOSProject”, lo and behold, the installation was successful.

Since OpenCV is in C++, I’m doing a crash course on that language by reading the book at this link.

While doing some research on what I’m trying to make, I found an amazing tutorial on how to do edge detection by the guy who developed a sudoku game app. Also found a very reasonably priced and highly rated book for OpenCV for iOS tutorial.

Day 54 – Make my first iPhone app in 16 weeks (Custom Camera)

This is part of “Make my first iPhone app in 16 weeks” series. AVCaptureConnection – Represents the connection between AVCaptureInput and AVCaptureOutput. AVCaptureStillImageOutput – Source for photos

AVCaptureSession – Central coordinating object to manage data capture from input to output. cameraOverlayView

  1. Send “beginConfiguration” message to session
  2. Lock the device for configuration to avoid conflict with other apps by sending “lockForConfiguration”
  3. Add inputs and outputs.
    1. Ask if a media type exists by sending “hasMediaType”
  4. Set sessionPreset to “AVCaptureSessionPresetPhoto” for capturing photos
    1. Ask if a preset exists by sending “supportsAVCaptureSessionPreset”
  5. Set focus mode by sending the following two methods: setFocusPointOfInterest & setFocusMode
    1. Ask if focusPointOfInterest and focusMode are supported by sending “focusPointOfInterestSupported” and “isFocusModeSupported”
    2. Ask if the device has finished focusing, using “adjustingFocus”
  6. Set exposure mode the same way you adjust focusMode. Just change “focus” to “exposure”
  7. Set flash mode: AVCaptureFlashModeOff/On/Auto
  8. Set white balance the same way you adjust focusMode. Just change “focus” to “whiteBalance”
  9. Lock the device for configuration to avoid conflict with other apps by sending “unlockForConfiguration”
  10. Send “commitConfiguration” message to session
  11. Send “startRunning” message to start capturing footage and “stopRunning” message to stop recording.
  12. Capture a still image by sending “captureStillImageAsynchronouslyFromConnection:completionHandler:” message to AVCaptureConnection

Day 53 – Make my first iPhone app in 16 weeks (Blac)

This is part of “Make my first iPhone app in 16 weeks”

I have so far finished the Stanford iOS course and completed every assignment that was given. I decided to go ahead and come up with my first project idea. Since I love art and sharing my artwork, I will make an app that can facilitate that process. So here comes “Blac” – Stop motion video maker to show the progress on an artwork from a blank paper to a masterpiece.

I made a paper mockup using this wonderful iOS app called POP (Prototyping on Paper). To view, click here. As you can see, it is a very simple 6-page app.

 

Day 50 to 52 – Make my first iPhone app in 16 weeks (Code Compilation)

This is part of “Make my first iPhone app in 16 weeks” series.

After grappling with Cocoa framework, I realized there are preset objects that you can/must use to accomplish a task. Also there are very particular ways that those objects need to be coded to properly function. So I decided to make a code compilation that I can refer back to at any moment.

See GitHub

Another very useful function in Xcode is to make code snippets out of commonly used methods. Steps it takes to create a code snippet takes some getting used to:

  1. Highlight the portion of code you would like to reuse
  2. Click and hold down until the cursor turns into a black pointer
  3. Drag it into the Code Snippet library in the bottom right hand corner
  4. Prefix the portion of the code that needs to be adjusted for later use with <# and suffix with #>. Watch anything between those two symbols turn into blue word blob.
XCode Code Snippet

XCode Code Snippet

Day 47 – Make my first iPhone app in 16 weeks (CGAffineTransform)

This is part of “Make my first iPhone app in 16 weeks” series.

All views have “transform” property. If not transformation has been applied to the view, the transform property is set to be “Identity Matrix” – constant “CGAffineTransformIdentity“.

  1. Translate Transformation (Move)
    1. CGAffineTransformTranslate (transform property, x units to move, y units to move)
    2. CGAffineTransformMakeTranslation ( x units to move, y units to move) – assumes CGAffineTransformIdentity
  2. Rotation Transform (Spin)
    1. CGAffineTransformRotate (transform property,radiance = (M_PI * degree / 180.0))
    2. CGAffineTransformMakeRotation (radiance = (M_PI * degree / 180.0) – assumes CGAffineTransformIdentity
  3. Scale Transform (Size)
    1. CGAffineTransformScale (transform property, x scale, y scale)
    2. CGAffineTransformMakeScale (x scale, y scale) – assumes CGAffineTransformIdentity
  4. Undo
    1. CGAffineTransformInvert (CGAffineTransform you want to revert)
  5. Cumulative Transform (Multiple transformations in one go)
    1. CGAffineTransformConcat (CGAffineTransform to be added to, CGAffineTransform to add)

Things to note!

  • All transformation happens based on the center of the view
  • Order matters

Continue reading