Friday, 18 November 2011

iphone snippet

//For the Invert of any object in iphone
UIGraphicsBeginImageContext(rgbImg.image.size);
    CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeCopy);
    UIImage * image=rgbImg.image;
    [image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
    CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeDifference);
    CGContextSetFillColorWithColor(UIGraphicsGetCurrentContext(),[UIColor whiteColor].CGColor);
    CGContextFillRect(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, image.size.width, image.size.height));
    rgbImg.image= UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();


//For the Black & White of the image
UIImage *originalImage = rgbImg.image; // this image we get from UIImagePickerController
    CGColorSpaceRef colorSapce = CGColorSpaceCreateDeviceGray();
    CGContextRef context = CGBitmapContextCreate(nil, originalImage.size.width, originalImage.size.height, 8, originalImage.size.width, colorSapce, kCGImageAlphaNone);
    CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
    CGContextSetShouldAntialias(context, NO);
    CGContextDrawImage(context, CGRectMake(0, 0, originalImage.size.width, originalImage.size.height), [originalImage CGImage]);
    CGImageRef bwImage = CGBitmapContextCreateImage(context);
    CGContextRelease(context);
    CGColorSpaceRelease(colorSapce);
    rgbImg.image = [UIImage imageWithCGImage:bwImage]; // This is result B/W image.
    CGImageRelease(bwImage);


//For the mirrored image orientation
if (mirrorOn==NO) {
       rgbImg.image = [UIImage imageWithCGImage:rgbImg.image.CGImage scale:rgbImg.image.scale orientation:UIImageOrientationUpMirrored];
       mirrorOn=YES;

    }
   else if (mirrorOn==YES) {
       mirrorOn=NO;
       rgbImg.image = [UIImage imageWithCGImage:rgbImg.image.CGImage scale:rgbImg.image.scale orientation:UIImageOrientationUp];

    }


//For the uiimage+extra classes for the brightness and contrast

https://github.com/coryleach/UIImageAdjust

//For the uiimage sepia and more

https://github.com/Nyx0uf/NYXImagesUtilities/tree/master/Categories

//For the paint in iphone

http://www.bogotobogo.com/XcodeSDK-Chapter11.html

//Dropdown in iphone
http://kshitizghimire.com.np/dropdown-in-iphoneipad/

//Lazy loading of tableview in iphone

http://kshitizghimire.com.np/lazy-loading-custom-uitableviewcell/

//TwitPic

http://www.techotopia.com/index.php/An_Example_iPhone_iOS_5_TWTweetComposeViewController_Twitter_Application

//For Image Cropping

- (UIImage*)imageByCropping:(UIImage *)imageToCrop toRect:(CGRect)rect
{
    CGImageRef imageRef = CGImageCreateWithImageInRect([imageToCrop CGImage], rect);
    UIImage *cropped = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);   
   
    return cropped;
}


//For imageview animation like crossDissolve

#import <QuartzCore/QuartzCore.h>
...
imageView.image = [UIImage imageNamed:(i % 2) ? @"3.jpg" : @"4.jpg"];

CATransition *transition = [CATransition animation];
transition.duration = 1.0f;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionFade;

[imageView.layer addAnimation:transition forKey:nil];

//For memory warning issues
http://akosma.com/2009/01/28/10-iphone-memory-management-tips/

//Cook book Example
http://code.google.com/p/iphone-sdk-programming-book-code-samples/downloads/list

//Send sms from the iphone ipad
- (void)sendSMS
{
    if ([MFMessageComposeViewController canSendText])
    {
        MFMessageComposeViewController *messageView = [[MFMessageComposeViewController alloc] init];
        messageView.messageComposeDelegate = self;
       
         [self presentModalViewController:messageView animated:YES];
        [messageView release];
    }
    else {
        [appDel showAlert:@"Oops" message:@"You can't send message"];
    }
   
}
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result {
  //  SMS.hidden = NO;
    switch (result)
    {
        case MessageComposeResultCancelled:
           // SMS.text = @"Result: canceled";
            NSLog(@"Result: canceled");
            break;
        case MessageComposeResultSent:
         //   SMS.text = @"Result: sent";
            NSLog(@"Result: sent");
            break;
        case MessageComposeResultFailed:
          //  SMS.text = @"Result: failed";
            NSLog(@"Result: failed");
            break;
        default:
         //   SMS.text = @"Result: not sent";
            NSLog(@"Result: not sent");
            break;
    }
   
    [self dismissModalViewControllerAnimated:YES];
   
}

//Send mail from the iphone ipad
-(IBAction)sendEmail
{
   
    //disappear=YES;
    if([MFMailComposeViewController canSendMail] == false) {
        UIAlertView *view = [[UIAlertView alloc] initWithTitle:@"Error" message:@"The device cannot currently send email." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [view show];
        [view release];
        return;
    }
    else{
        Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
        if (mailClass != nil)
        {
            // We must always check whether the current device is configured for sending emails
            if ([mailClass canSendMail])
            {
                [self displayComposerSheet];
            }
            else
            {
                [self launchMailAppOnDevice];
            }
        }
        else
        {
            [self launchMailAppOnDevice];
        }
    }
   
}
-(void)displayComposerSheet
{
   
    MFMailComposeViewController *mailView = [[MFMailComposeViewController alloc] init];
    mailView.mailComposeDelegate = self;
    //SharedManager *sm = [SharedManager sharedInstance];
    [mailView setSubject:@"Hello from "];//[sm.text substringToIndex:20]];
   
    [mailView setMessageBody:@"You can know more from this url!" isHTML:YES];
   
    NSData *attachmentData = UIImageJPEGRepresentation(getImg, 1.0);
    UIImage *image=getImg;
    [mailView addAttachmentData:attachmentData mimeType:@"image/png" fileName:[NSString stringWithFormat:@"%@",image]];
     [self presentModalViewController:mailView animated:YES];
    [mailView release];
   
}


#pragma mark -
#pragma mark Workaround

// Launches the Mail application on the device.
-(void)launchMailAppOnDevice
{    
    MFMailComposeViewController *mailView = [[MFMailComposeViewController alloc] init];
    mailView.mailComposeDelegate = self;
    [mailView setSubject:@"Hello from  "];    
    [mailView setMessageBody:@"You can know more from this url!" isHTML:YES];
   
    NSData *attachmentData = UIImageJPEGRepresentation(getImg, 1.0);
    UIImage *image=getImg;
    [mailView addAttachmentData:attachmentData mimeType:@"image/png" fileName:[NSString stringWithFormat:@"%@",image]];
    [self presentModalViewController:mailView animated:YES];
    [mailView release];
   
   
   
}
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
    // Dismiss Mail View
    [self dismissModalViewControllerAnimated:YES];
   
    switch (result)
    {
        case MFMailComposeResultCancelled:
            NSLog(@"Mail send canceled");
            self.navigationController.navigationBar.hidden =YES;
            break;
        case MFMailComposeResultSent:
            [appDel showAlert:@"Mail sent successfully." message:@"Success"];
            NSLog(@"Mail send successfully");
            break;
        case MFMailComposeResultSaved:
            [appDel showAlert:@"Mail saved to drafts successfully." message:@"Mail saved"];
            NSLog(@"Mail Saved");
            break;
        case MFMailComposeResultFailed:
            [appDel showAlert:[NSString stringWithFormat:@"Error:%@.", [error localizedDescription]] message:@"Failed to send mail"];
            NSLog(@"Mail send error : %@",[error localizedDescription]);
            break;
        default:
            break;
    }
}

// For SCListner class
https://github.com/stephencelis/sc_listener
//For mic Detection program
http://mobileorchard.com/tutorial-detecting-when-a-user-blows-into-the-mic/


//For the drawing the rectangle lines on the any object
- (void)drawRect  {
    UIGraphicsBeginImageContext(self.view.frame.size);
   
    CGContextRef currentContext = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(currentContext, 3.0); //or whatever width you want
    CGContextSetRGBStrokeColor(currentContext, 0.0, 0.0, 0.0, 1.0);
   
    CGRect myRect = CGContextGetClipBoundingBox(currentContext);
    //printf("rect = %f,%f,%f,%f\n", myRect.origin.x, myRect.origin.y, myRect.size.width, myRect.size.height);
   
    CGContextStrokeRect(currentContext, myRect);
    UIImage *backgroundImage = (UIImage *)UIGraphicsGetImageFromCurrentImageContext();
    UIImageView *myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    [myImageView setImage:backgroundImage];
    [self.view addSubview:myImageView];
    [backgroundImage release];
   
    UIGraphicsEndImageContext();
}

//For the Border of the imageview in iphone

 [imageView.layer setBorderColor: [[UIColor redColor] CGColor]];
    [imageView.layer setBorderWidth: 2.0];

//Different Example for ios
http://projectswithlove.com/projects/