code

Changing Background Color and Section Header Text Color in a Grouped-style UITableView

While working on an iPhone application, I decided I wanted to change the colors of the background and section headers of a UITableView with the UITableViewStyleGrouped style. It took a lot more work than I expected, so I'm sharing what I learned with anyone else who needs to do this.

Adding a Custom View to an NSStatusItem

My Menubar Countdown application uses an NSStatusItem to display itself in the menu bar. I recently had to add a custom view to that status item, and thought I'd share what I learned about the process here.

Saving a View as a Photo in an iPhone App

For an iPhone app that I'm working on, I want to be able to save the screen image to the Photos album. My first attempt at this was complicated: I created a color space, a bitmap context, a CGImage, and finally a UIImage, copying and pasting most of the code from the Quartz 2D Programming Guide. Unfortunately, it didn't work; I kept getting BAD_ACCESS signals when I called UIImageWriteToSavedPhotosAlbum(), even though it looked to me like everything was correct.

After Googling for a bit for known issues with UIImageWriteToSavedPhotosAlbum, I ran across a far easier solution to the problem. Here are the methods I ended up with:

// Create an image for the view and save it to the Photos library
- (void)savePhotoOfView:(UIView *)view
{
    UIGraphicsBeginImageContext(view.bounds.size);
    [view drawRect:view.bounds];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    UIImageWriteToSavedPhotosAlbum(image,
                                   self,
                                   @selector(savedPhotoImage:didFinishSavingWithError:contextInfo:),
                                   NULL);
}

// Called by UIImageWriteToSavedPhotosAlbum() when it completes
- (void)   savedPhotoImage:(UIImage *)image
  didFinishSavingWithError:(NSError *)error
               contextInfo:(void *)contextInfo
{    
    NSString *message = @"This image has been saved to your Photos album";
    if (error) {
        message = [error localizedDescription];
    }
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
                                                    message:message
                                                   delegate:nil
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
    [alert show];
    [alert release];
}

These just call the view's drawRect method to create an image, save the image to the Photos library, and then pop up an alert box to let the user know what happened.

Syndicate content