Reusable Snippets

Shake reset

Shake reset. Actually the snippet really just for responding to a shake motion. The snippet was used to reset an image, but can be changed out to respond to anything. Another objective-c snip. There a comment in the block of code below where the change can occur.

--

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {

}

// Shaking here resets an image but it can do anything, just change out self
// resetImage to something else.
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
if (motion = UIEventSubtypeMotionShake ) {
[self resetImage];
}
}

- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event {

}

Filed under  //   Objective- C   iPhone   motion  
Posted May 6, 2010

Capture UIView

A little snip for creating an image from an UIView. Much like a snapshot from an view or rect. and exports new image to the photo album.

 

 

#import <QuartzCore/QuartzCore.h>

 

- (UIImage*)captureView:(UIView *)view {

CGRect rect = [[UIScreen mainScreen] bounds];  

UIGraphicsBeginImageContext(rect.size);  

CGContextRef context = UIGraphicsGetCurrentContext();  

[view.layer renderInContext:context];  

UIImage *img = UIGraphicsGetImageFromCurrentImageContext();  

UIGraphicsEndImageContext();  

return img;

}

 

- (void)saveScreenshotToPhotosAlbum:(UIView *)view {

UIImageWriteToSavedPhotosAlbum([self captureView:view], nil, nil, nil);

}

 

Filed under  //   Capture   Core Graphics   Images   Objective- C   iPhone  
Posted May 5, 2010