Saturday, March 10, 2012

The Maximum Number of Rows in UITableViewController

I am quite interested in find the number of rows that UITableViewController supports. The result is between 40,000,000 and 50,000,000:


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return 40000000;
}

A table view with 40,000,000 rows works fine but with 50,000,000 rows it does not.

Saturday, February 25, 2012

iOS: Run-time error when using a class category in a different static library

When you are calling a member function defined in a class category (https://developer.apple.com/library/ios/#documentation/cocoa/conceptual/objectivec/chapters/occategories.html) from a different static library, you might see a run-time exception of "selector not recognized".

This is a well-know issue with Xcode and iOS. There is a technical note from Apple to describe the problem and the workaround by using the -all_load flag or the -force_load flag followed by the archive path:

https://developer.apple.com/library/mac/#qa/qa1490/_index.html

There is also another workaround without defining the flag. In the file that define the class category, add a C function to the .h and .m, and call this function anywhere in the project that you call the member function of the class category. For example, if you have a class category called UIImage (A) in UIImage+A.h and UIImage+A.m, you can add a function like this:


UIImage+A.h
------------------
// This function needs to be called in the file that uses UIImage (A)
void forceLoadUIImageCategoryA(void);

@interface UIImage (A)
...
@end


UIImage+A.m
------------------
void forceLoadUIImageCategoryA(void)
{
    // Do nothing
}

@implementation UIImage (A)
...
@end


In the file that uses any member functions of UIImage (A), call forceLoadUIImageCategoryA(); anywhere. Calling this C function will make Xcode link the .h and .m.

Xcode warning: no previous prototype for function ...

This warning appears when you define a C-style function like this:

In .h:
void foo();

In .m:
void foo()
{
}

However, according to C standard, you should define the function with the void keyword if there is no parameters in your function:

In .h:
void foo(void);

In .m:
void foo(void)
{
}

Sunday, February 12, 2012

Why UIView's autoresizingMask does not work

I was trying to use to autoresizingMask in a UILabel to resize the width when the device is rotated. Following my first instinct, I wrote label.autoresizingMask = UIViewAutoresizingFlexibleWidth. However, the label didn't resize. Why?

There are a few values for the mask such as UIViewAutoresizingNone, UIViewAutoresizingFlexibleRightMargin, UIViewAutoresizingFlexibleLeftMargin, and UIViewAutoresizingFlexibleHeight. I tried to use UIViewAutoresizingFlexibleRightMargin or UIViewAutoresizingFlexibleLeftMargin but there was no difference.

Wait a minute. If we need to set those values to enable flexible margins, width, and height, what does it mean if we leave them unset? If I only set the autoresizingMask property to UIViewAutoresizingFlexibleWidth, it actually means Flexible Width AND Fixed Left Margin AND Fixed Right Margin. Even though the width is flexible, the fixed left and right margins do not allow the flexible width to expand or shrink.

Now, it is clear that when we need to set flexible width or height, we must also set  at least a corresponding margin to be flexible at the same time. For example, if we want the label to expand the end of the text (assuming that the text goes from left to right) when the width changes, we should set autoresizingMask to UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin so that the label can expand it's width at the right margin.

Sunday, February 5, 2012

Use of undeclared identifier 'kUTTypeImage'

This constant is declared in UTCoreTypes.h in the MobileCoreServices framework. In the .m files that you use the constant kUTTypeImage, import the above .h file:

   #import <MobileCoreServices/UTCoreTypes.h>

In the application project, you also need to include the MobileCoreServices framework.

Thursday, January 12, 2012

Rendering images in a background thread in Qt

QImage instead of QPainter should be used. According to the QImage class reference, QPainter can be used to draw directly onto images. When using QPainter on a QImage, the painting can be performed in another thread than the current GUI thread.

Render-to-texture in OpenSceneGraph

 // Create a render-to-texture camera with an absolute
 // reference frame and pre-render
 osg::ref_ptr<osg::Camera> camera = new osg::Camera();
 camera->setReferenceFrame(osg::Camera::ABSOLUTE_RF);
 camera->setRenderOrder(osg::Camera::PRE_RENDER);

 // Set the camera's projection matrix to reflect the
 // world of the geometries that will appear in the texture
 camera->setProjectionMatrixAsOrtho2D(minx, maxx, miny, maxy);

 //Set the camera's viewport to the same size of the texture
 camera->setViewport(0, 0, textureWidth, textureHeight);

 // Set the camera's clear color and masks
 camera->setClearColor(clearColor);
 camera->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

 //Create the texture image
 osg::ref_ptr<osg::Image> image = new osg::Image();
 image->allocateImage(textureWidth,
                      textureHeight,
                      1,   // 2D texture is 1 pixel deep
                      GL_RGBA,
                      GL_UNSIGNED_BYTE);
 image->setInternalTextureFormat(GL_RGBA8);

 // Create the texture object and set the image
 osg::ref_ptr<osg::Texture2D> texture2D = new osg::Texture2D();
 texture2D->setImage(image);

 // Attach the texture to the camera. You can also attach
 // the image to the camera instead. Attaching the image may
 // work better in the Windows Remote Desktop
 camera->attach(osg::Camera::COLOR_BUFFER, texture2D.get());

 // Add the geometries that will appear in the texture to the camera
 camera->addChild(geometriesInTexture);

 // Create a geode to display the texture
 osg::ref_ptr<osg::Geode> geode = new osg::Geode();

 // Create a geometry for the geode
 osg::ref_ptr<osg::Geometry> geometry = new osg::Geometry();
 geode->addDrawable(geometry);

 // The coordinates can be different if we want to display the
 // texture at a location different from the coordinates of the
 // geometries inside the texture
 osg::ref_ptr<osg::Vec3Array> vertexArray = new osg::Vec3Array();
 vertexArray->push_back(osg::Vec3(minx, miny, minz));
 vertexArray->push_back(osg::Vec3(maxx, miny, minz));
 vertexArray->push_back(osg::Vec3(maxx, maxy, minz));
 vertexArray->push_back(osg::Vec3(minx, maxy, minz));
 geometry->setVertexArray(vertexArray);

 // The geometry color for the texture mapping should be white
 // (1.f, 1.f, 1.f, 1.f) so that color blending won't affect
 // the texture real color
 osg::ref_ptr<osg::Vec4Array> colorArray = new osg::Vec4Array();
 colorArray->push_back(osg::Vec4(1.f, 1.f, 1.f, 1.f));
 geometry->setColorArray(colorArray);
 geometry->setColorBinding(osg::Geometry::BIND_OVERALL);

 // We are using the entire texture by using the four corners
 // of the texture coordinates
 osg::ref_ptr<osg::Vec2Array> texCoordArray = new osg::Vec2Array();
 texCoordArray->push_back(osg::Vec2(0.f, 0.f));
 texCoordArray->push_back(osg::Vec2(1.f, 0.f));
 texCoordArray->push_back(osg::Vec2(1.f, 1.f));
 texCoordArray->push_back(osg::Vec2(0.f, 1.f));
 geometry->setTexCoordArray(0, texCoordArray);

 // We always create a square texture.
 geometry->addPrimitiveSet(
    new osg::DrawArrays(GL_TRIANGLE_FAN, 0, 4));

 // Add the texture to the geode
 geode->getOrCreateStateSet()->setTextureAttributeAndModes(
    0, texture2D, osg::StateAttribute::ON);

 // Make sure that we are using color blending
 // and the transparent bin, since the features may not
 // cover the entire texture and the empty
 // space should be transparent.
 geode->getOrCreateStateSet()->setRenderingHint(
    osg::StateSet::TRANSPARENT_BIN);
 osg::ref_ptr<osg::BlendFunc> blendFunc = new osg::BlendFunc;
 blendFunc->setFunction(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
 geode->getOrCreateStateSet()->setAttributeAndModes(blendFunc);

 // Add both the rtt camera and the geode to the scene graph
 sceneGraph->addChild(camera);
 sceneGraph->addChild(geode);

* NOTE: If we only want to render one frame on the texture, we can remove the camera from the scene graph after the frame is rendered. The geode will still display the static texture in the scene graph properly.