Thursday, June 10, 2010

iPhone dev Stupidity 130: antialiasing on a rotated view

from stackoverflow:

For a UIImageView, add a one-pixel transparent border around the image. This will cause the edges of your image to be on the "inside", and because the inside pixels are interpolated when rotating, the borders will magically become antialiased. This also works for rotating UIButtons that have a custom image.

Wednesday, June 09, 2010

iPhone dev Stupidity 129: -all_load -ObjC linker flag in Xcode

From Apple Q&A

Q: Why do I get a runtime exception of "selector not recognized" when linking against an Objective-C static library that contains categories?

A: The "selector not recognized" runtime exception occurs due to an issue between the implementation of standard UNIX static libraries, the linker and the dynamic nature of Objective-C. Objective-C does not define linker symbols for each function (or method, in Objective-C) - instead, linker symbols are only generated for each class. If you extend a pre-existing class with categories, the linker does not know to associate the object code of the core class implementation and the category implementation. This prevents objects created in the resulting application from responding to a selector that is defined in the category.

To resolve this issue, the target linking against the static library must pass the -ObjC option to the linker. This flag causes the linker to load every object file in the library that defines an Objective-C class or category. While this option will typically result in a larger executable (due to additional object code loaded into the application), it will allow the successful creation of effective Objective-C static libraries that contain categories on existing classes. Follow these steps to pass -ObjC to the linker:

  1. In Xcode, double-click the target's name under "Targets" in the Project window.

  2. Choose the Build pane from the ensuing Info window.

  3. Scroll down to the Other Linker Flags build setting under the Linking collection and set its value to -ObjC.

Figure 1: Target Build pane: Other Linker Flags

Figure 1, Target Build pane: Other Linker Flags

IMPORTANT: For 64-bit and iPhone OS applications, there is a linker bug that prevents -ObjC from loading objects files from static libraries that contain only categories and no classes. The workaround is to use the -all_load or -force_load flags.

-all_load forces the linker to load all object files from every archive it sees, even those without Objective-C code. -force_load is available in Xcode 3.2 and later. It allows finer grain control of archive loading. Each -force_load option must be followed by a path to an archive, and every object file in that archive will be loaded.


iPhone dev Stupidity 128: Photo orientation issue in Three20’s TT

from stackoverflow:

You probably set size: attribute of the photoSource to some wrong value.

Edit: Unfortunately i don't have another suggestion for your problem, however i have a warning. You should definitely scale your image before display, 1536x2048 is way too large for iphone to handle, and totally unnecessary for a 320x480 screen. Otherwise you are sure to have app crashes due to low memory in the future - if not now.

iPhone dev Stupidity 127: How Three20 works

1. Create TTThumbesViewController via URL: "tt:\\phototest2"


2. When is the TTURLMap setuped?


3. And mapped to class


  [map from:@"*" toViewController:[TTWebController class]];

  [map from:@"tt://catalog" toViewController:[CatalogController class]];

  [map from:@"tt://photoTest1" toViewController:[PhotoTest1Controller class]];

  [map from:@"tt://photoTest2" toViewController:[PhotoTest2Controller class]];


4. How TTThumbsView get created?



5. URL protocol


The URL "protocol" for the bundled files is bundle:// 
while the one for the files in the documents directory is documents://

6. A Tutorial on How to create a photo browser using Three20

Most of the code is just copied from the TTCatalog sample code.







iPhone dev Stupidity 126: private folder for images

Saving image directly to the folder

//I do this in the didFinishPickingImage:(UIImage *)img method

NSData* imageData = UIImageJPEGRepresentation(img, 1.0);


//save to the default 100Apple(Camera Roll) folder.  

[imageData writeToFile:@"/private/var/mobile/Media/DCIM/100APPLE/customImageFilename.jpg" atomically:NO];

The photos there are all named in IMG_####.JPG format.

There's also a .MISC folder which contains the thumbnail for each image.

To inspect the folder: 

NSArray *directoryContent = [[NSFileManager defaultManager] directoryContentsAtPath:@"/private/var/mobile/Media/DCIM/100APPLE/.MISC"];

for(NSString * dir in directoryContent){

NSLog(dir);

}



iPhone dev Stupidity 125: lack of EXIF with the iPhone API

UIImageWriteToSavedPhotoAlbum will strip all the comments/etc off. Tried several way and failed, and read this:

"The problem actually lies with UIImage, the data parameter that feeds into UIImageWriteToSavedPhotosAlbum. UIImage actually strips off any extra EXIF information on the image, even if you try to add them with library such as iphone-exif.

To save an image with EXIF using iphone-exif, you would have to add the extra EXIF information to an NSData representation of the UIImage and write the file directly to disk. However, with the available API, you can only write to the PhotoAlbum using UIImageWriteToSavedPhotosAlbum, which only accept UIImage. Once you convert the NSData back to an UIImage, all the EXIF information is lost again! And we are back to square one."

iPhone dev Stupidity 124: Rotating view around arbitrary point th

From here:

"I have a UIImageView that I want to rotate around a given point. By default, the layer will rotate around the center of the UIImageView. Trying to change this anchor point involves a bit of work, either move at the beginning and reposition all the view elements, or translating the view, rotating, and translating back. The last option works fine, unless you are trying to use core animation to smoothly rotate the view.

I found out a much easier solution to the whole problem. You could call it a workaround, but it works well without disturbing the view you want rotated.

Basically, you create a new UIView. Place the center point where you want the rotation to occur. Change the size of the view so that the view you want rotated will fit inside. Make your view (you want rotated) a subview of the new view. Now, just rotate the new view, and your subview will be rotated correctly.

Example: lets say we have a UIImageView that is 320x480 (the size of the whole iPhone screen). We want it to rotate from the bottom center instead of its middle. Create a new UIView that is 0,0,320,960. This is basically double the height of the screen, positioned so the bottom half is off the screen, which places the center point right where we want it. Now place your UIImageView inside the new UIView in the top half (visible on the screen.) Now when you rotate the UIView, it will pivot on the bottom center of the screen, and your UIImageView will rotate along with it, as expected."

Instead of adding a parent view, you can also adjust the size of the view itself if feasible.

iPhone dev Stupidity 123: How to launch iPhone Camera on viewDidL


You should do it in viewWillAppear:, or viewDidAppear: if the first doesn't work. trying to do it in viewDidLoad won't work because that is called after the view is first created, and the view isn't a subview of anything else at that point. 

As far as i understand it, in order to call presentModalViewController on self, the view must at some level be displayed in the UIWindow.

And some solutions on camera shutter animation:



The movie files are too big ~ 7MB ~ to fit here.


UIImageView animationImages

imageView.animationImages = [NSArray arrayWithObjects:
                                     
[UIImage imageNamed:@"cameraScreenOverlay1.png"],
                                     
[UIImage imageNamed:@"cameraScreenOverlay2.png"],
                                     
[UIImage imageNamed:@"cameraScreenOverlay3.png"],
                                     
[UIImage imageNamed:@"cameraScreenOverlay4.png"],
                                     
[UIImage imageNamed:@"cameraScreenOverlay4.png"],
                                     
[UIImage imageNamed:@"cameraScreenOverlay4.png"],
                                     
[UIImage imageNamed:@"cameraScreenOverlay3.png"],
                                     
[UIImage imageNamed:@"cameraScreenOverlay2.png"],
                                     
[UIImage imageNamed:@"cameraScreenOverlay1.png"],
                                     
nil];
        imageView
.animationDuration = 1.0;
        imageView
.animationRepeatCount = 0;
       
[imageView startAnimating];

doesn't work when imageView is used as an overlay view above camera.


Not sure how he had done it - snapshoting each frame of the camera animation?

iPhone dev Stupidity 122: Customize bars, buttons, status bar

1. UINavigationBar with solid color or image background 

use category to redefine the drawRect: method.



Gradient rounded buttons from "cocoa is my girl friends":



4. Hiding status bar via .plist

iPhone dev Stupidity 121: Drawing gloss gradients in CoreGraphics

 
iPhone version at here.