Category Archives: [Code] iOS

Code iOS : Get pixel data from a UIImage (Cocoa Touch) or CGImage (Core Graphics)


Code iOS : Get pixel data from a UIImage (Cocoa Touch) or CGImage (Core Graphics)

อ่านค่าสี pixel จากรูปภาพ

<code>+ (NSArray*)getRGBAsFromImage:(UIImage*)image atX:(int)xx andY:(int)yy count:(int)count
{
NSMutableArray *result = [NSMutableArray arrayWithCapacity:count];

// First get the image into your data buffer
CGImageRef imageRef = [image CGImage];
NSUInteger width = CGImageGetWidth(imageRef);
NSUInteger height = CGImageGetHeight(imageRef);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
unsigned char *rawData = (unsigned char*) calloc(height * width * 4, sizeof(unsigned char));
NSUInteger bytesPerPixel = 4;
NSUInteger bytesPerRow = bytesPerPixel * width;
NSUInteger bitsPerComponent = 8;
CGContextRef context = CGBitmapContextCreate(rawData, width, height,
bitsPerComponent, bytesPerRow, colorSpace,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);

CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
CGContextRelease(context);

// Now your rawData contains the image data in the RGBA8888 pixel format.
int byteIndex = (bytesPerRow * yy) + xx * bytesPerPixel;
for (int ii = 0 ; ii < count ; ++ii)
{
CGFloat red   = (rawData[byteIndex]     * 1.0) / 255.0;
CGFloat green = (rawData[byteIndex + 1] * 1.0) / 255.0;
CGFloat blue  = (rawData[byteIndex + 2] * 1.0) / 255.0;
CGFloat alpha = (rawData[byteIndex + 3] * 1.0) / 255.0;
byteIndex += 4;

UIColor *acolor = [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
[result addObject:acolor];
}

free(rawData);

return result;
}</code>

Code iOS : Storyboard Instantiate ViewController


Code iOS : Storyboard Instantiate ViewController


[self.storyboard instantiateViewControllerWithIdentifier:@"identifier of viewcontroller"];

อ่านรายละเอียดเพิ่มเติม

adaydesign :)

Code iOS : Toggle UI Orientation


Code iOS : Toggle UI Orientation
การกำหนดทิศทาง แนวของหน้าจอว่าจะให้แสดงในแบบไหน

if(UIInterfaceOrientationIsPortrait(self.interfaceOrientation)){
 [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];
}else{
 [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait];
}

Code iPhone: Sort an Array of Custom Object using NSSortDescriptor


Code iPhone: Sort an Array of Custom Object using NSSortDescriptor

+(NSArray*)sortArray:(NSArray*)arrayIn withKey:(NSString*)key ascending:(BOOL)asc{
 NSSortDescriptor *sortDescriptor = [[[NSSortDescriptor alloc]
 initWithKey:key ascending:asc] autorelease];
 NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
 NSArray *sortedArray = [arrayIn sortedArrayUsingDescriptors:sortDescriptors];

 return sortedArray;
}

Detail

Reference

adaydesign :)

Code iPhone: Load Photo from Photos Album (Full Function)


Code iPhone: Load Photo from Photos Album (Full Function)

In .h file (header/@interface)


#import <AssetsLibrary/AssetsLibrary.h>

typedef void (^ALAssetsLibraryAssetForURLResultBlock)(ALAsset *myasset);

typedef void (^ALAssetsLibraryAccessFailureBlock)(NSError *error);

//function

-(void)loadPhotoFromURL:(NSURL*)imgURL thumbnail:(BOOL)useThumbnail showIn:(UIImageView*)imView;

Note! import existing framework: AssetsLibrary.framework

Read the rest of this entry

Code iPhone: Flip Navigation Controller


Code iPhone: Flip Navigation Controller


MyViewController *myView = [[MyViewController alloc] initWithWord:[myList objectAtIndex:indexPath.row]];

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration: 1];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.navigationController.view cache:NO];

[self.navigationController pushViewController:myView animated:YES];
[myView release];
[UIView commitAnimations];

Detail

adaydesign :)

Code iPhone: Crop UIImage


Code iPhone: Crop UIImage

UIImage *tmp = [UIImage imageNamed:@"image_name.png"];
CGRect rectCrop = CGRectMake(0, 0,w, h);//size of you want to crop this image (tmp)
CGImageRef imgRef = CGImageCreateWithImageInRect([tmp CGImage], rectCrop);
UIImage *image = [UIImage imageWithCGImage:imgRef];

เรากำหนดขนาดตามที่ต้องการได้ที่ตัวแปร rectCrop ซึ่งจะกำหนด x y width height ของรูปอ้างอิง

Detail

adaydesign :)

%d bloggers like this: