Wednesday, November 21, 2012

Generate a random number and use it as the index. Example:


#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{
    @autoreleasepool {
        NSArray *array = [NSArray arrayWithObjects: @"one", @"two", @"three", @"four", nil];
        NSUInteger randomNumber;
        int fd = open("/dev/random", O_RDONLY);
        if (fd != -1) {
            read(fd, &randomNumber, sizeof(randomNumber));
            close(fd);
        } else {
            fprintf(stderr, "Unable to open /dev/random: %s\n", strerror(errno));
            return -1;
        }
        double scaledRandomNumber = ((double)randomNumber)/NSUIntegerMax * [array count];
        NSUInteger randomIndex = (NSUInteger)floor(scaledRandomNumber);
        NSLog(@"random element: %@", [array objectAtIndex: randomIndex]);
    }
    return 0;
}

URL GET/POST Request objective-c


I have to send get or post request to localhost:
<?php
 if(@$_GET['option']) {
  echo "You said \"{$_GET['option']}\"";
 }else if(@$_POST['option']) {
  echo "You said \"{$_POST['option']}\"";
 }
?>
ive using this code:
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://localhost/wsh/index.php?option=Hello"]];
 NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
 NSString *get = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
it works, but one time in code. if ill do it another one time, application has terminate.

Monday, November 12, 2012

Kết nối java với SQL server


1. Kết nối thông qua ODBC

a. Dùng Data Source Name

Thiết lập DataSource như sau :

Trên windows Vào menu Start->Settings->Control Panel->Administrative Tools->Data Sources (ODBC), kết quả như hình

[IMG]

Nhấn Nút Add, Tìm đến SQL Server, Nhấn Finish.

Nhập vào tên của Data Source, ví dụ là MyDSN, chọn server: nếu server ở localhost thì có thể gõ vào dấu ., nếu có nhiều instances của sqlserver thì phải gõ vào tên_máy_tính/tên_instance. Nhấn Next 2 lần

Merging two images in objective-c

While researching about image manipulation for my next project, I came across an interesting piece of code, using which, we can merge two images into a single image. Here's the code:

-(UIImage*)mergeImage:(UIImage*)mask overImage:(UIImage*)source inSize:(CGSize)size
{
//Capture image context ref
UIGraphicsBeginImageContext(size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

//Draw images onto the context
[source drawInRect:CGRectMake(0, 0, source.size.width, source.size.height)]; 
[mask drawInRect:CGRectMake(0, 0, mask.size.width, mask.size.height)]; 

return viewImage;

}


Got the reference from here.

Objective C: Merge two images


+ (UIImage*)mergeImage:(UIImage*)first withImage:(UIImage*)second
{
 // get size of the first image
 CGImageRef firstImageRef = first.CGImage;
 CGFloat firstWidth = CGImageGetWidth(firstImageRef);
 CGFloat firstHeight = CGImageGetHeight(firstImageRef);
  
 // get size of the second image
 CGImageRef secondImageRef = second.CGImage;
 CGFloat secondWidth = CGImageGetWidth(secondImageRef);
 CGFloat secondHeight = CGImageGetHeight(secondImageRef);
  
 // build merged size
 CGSize mergedSize = CGSizeMake(MAX(firstWidth, secondWidth), MAX(firstHeight, secondHeight));
  
 // capture image context ref
 UIGraphicsBeginImageContext(mergedSize);
  
 //Draw images onto the context
 [first drawInRect:CGRectMake(0, 0, firstWidth, firstHeight)];
 [second drawInRect:CGRectMake(0, 0, secondWidth, secondHeight)];
  
 // assign context to new UIImage
 UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
  
 // end context
 UIGraphicsEndImageContext();
 
 return newImage;
}

Popular Posts