Category Archives: Code Snippet

Code Snippet

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 Android: Convert pixel to dip


Code Android: Convert pixel to dip
เปลี่ยนค่าของ Pixel ให้เป็น dip เพื่อนำมาใช้ในการเขียนโปรแกรมบนแอนดรอย


Resources r = context.getResources();

float wDip = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 95, r.getDisplayMetrics());

float hDip = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 102, r.getDisplayMetrics());

//use

convertView = new ImageView(context);

((ImageView)convertView).setLayoutParams(new GridView.LayoutParams((int)wDip,(int)hDip));

Read the rest of this entry

Code Android: Get version name


Code Android: Get version name

public static function String getVersionName(Context context, Class cls)
{
  try {
    ComponentName comp = new ComponentName(context, cls);
    PackageInfo pinfo = context.getPackageManager().getPackageInfo(comp.getPackageName(), 0);
    return pinfo.versionName;
  } catch (android.content.pm.PackageManager.NameNotFoundException e) {
    return null;
  }
}

Reference

adaydesign :)

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

%d bloggers like this: