Core Graphics相关的转换函数

来源:互联网 发布:mysql geometry 编辑:程序博客网 时间:2024/05/22 10:33

Convert image to/from text (Base64)

By Alex14. 三月 2010 15:56

#import "NSDataAdditions.h"

 
 
 

-(NSString *)getStringFromImage:(UIImage *)image{

if(image){

NSData *dataObj = UIImagePNGRepresentation(image);

return [dataObj base64Encoding];

else {

return @"";

}

}
 
//Convert back 

NSData *dataObj = [NSData dataWithBase64EncodedString:beforeStringImage];

UIImage *beforeImage = [UIImage imageWithData:dataObj];

 

NSDataAdditions.zip (2.80 kb)

Tags: uiimage, uiimage, uiimage, uiimage

General | General | General | General

Create screenshot in runtime (inside UIView)

By Alex7. 十一月 2009 10:35

UIGraphicsBeginImageContext(self.bounds.size);

[self.layer renderInContext:UIGraphicsGetCurrentContext()];

UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

Tags: uiview, uiview, uiimage, uiimage, uiview, uiimage, uiimage

General | General | General | General

Get pixel information from UIImage

By Alex4. 十月 2009 02:46

UIImage *uiImage = [UIImage imageNamed:@"myimage.png"];

CGImageRef image = uiImage.CGImage;

NSUInteger width = CGImageGetWidth(image);

NSUInteger height = CGImageGetHeight(image);

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

UInt8 * rawData = malloc(height * width * 4);

 

int bytesPerPixel = 4;

int bytesPerRow = bytesPerPixel * width;

 

NSUInteger bitsPerComponent = 8;

CGContextRef context1 = CGBitmapContextCreate(

rawData, width, height, bitsPerComponent, bytesPerRow, colorSpace,

kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big

);

CGColorSpaceRelease(colorSpace);

 

CGContextDrawImage(context1, CGRectMake(00, width, height), image);

CGContextRelease(context1);

 

//Now to get byte for ppixel you need to call

//rawData[x * bytesPerRow  + y * bytesPerPixel]


Tags: uiimage

General

Convert UIImage to NSData

By Alex2. 六月 2009 14:34

UIImage *img = [UIImage imageNamed:@"some.png"];

NSData *dataObj = UIImageJPEGRepresentation(img, 1.0);

Tags: nsdata, uiimage, nsdata

General

Save image to image library

By Alex12. 五月 2009 15:28

-(void)savePictureToLibrary{    

    UIImage *img = [[UIImage imageNamed:@"image.png"];

    UIImageWriteToSavedPhotosAlbum(img, self@selector(image:didFinishSavingWithError:contextInfo:), self);

}

 

 

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{

    NSString *str = @"Saved!!!";

    UIAlertView *alert = [[UIAlertView allocinitWithTitle:@"Saved." message:str delegate:selfcancelButtonTitle:nil otherButtonTitles:@"OK"nil];

    [alert show];

}


Tags: uiimage

General

Download image from HTTP server (faster)

By Alex9. 五月 2009 03:41

//This method works much faster then [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.google.com/intl/en_ALL/images/logo.gif"]];

//Also it works better on bad internet connections

NSMutableURLRequest *requestWithBodyParams = [NSMutableURLRequest requestWithURL:[NSURLURLWithString:@"http://www.google.com/intl/en_ALL/images/logo.gif"]];

NSData *imageData = [NSURLConnection sendSynchronousRequest:requestWithBodyParams returningResponse:nilerror:nil];

UIImage *image = [UIImage imageWithData:imageData];


Tags: uiimage, nsdata, nsmutableurlrequest

General

Resize image and keep aspect ratio

By Alex6. 五月 2009 06:33

//Resize image and keep aspect ratio

-(UIImage *)resizeImage:(UIImage *)image {

int w = image.size.width;

    int h = image.size.height

CGImageRef imageRef = [image CGImage];

int width, height;

int destWidth = 640;

int destHeight = 480;

if(w > h){

width = destWidth;

height = h*destWidth/w;

else {

height = destHeight;

width = w*destHeight/h;

}

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

 

CGContextRef bitmap;

bitmap = CGBitmapContextCreate(NULL, width, height, 84 * width, colorSpace,kCGImageAlphaPremultipliedFirst);

if (image.imageOrientation == UIImageOrientationLeft) {

CGContextRotateCTM (bitmap, M_PI/2);

CGContextTranslateCTM (bitmap, 0, -height);

else if (image.imageOrientation == UIImageOrientationRight) {

CGContextRotateCTM (bitmap, -M_PI/2);

CGContextTranslateCTM (bitmap, -width, 0);

else if (image.imageOrientation == UIImageOrientationUp) {

else if (image.imageOrientation == UIImageOrientationDown) {

CGContextTranslateCTM (bitmap, width,height);

CGContextRotateCTM (bitmap, -M_PI);

}

CGContextDrawImage(bitmap, CGRectMake(00, width, height), imageRef);

CGImageRef ref = CGBitmapContextCreateImage(bitmap);

UIImage *result = [UIImage imageWithCGImage:ref];

CGContextRelease(bitmap);

CGImageRelease(ref);

return result;

}

Tags: uiimage

General

Add text to image (UIImage)

By Alex5. 五月 2009 13:37

//Add text to UIImage

-(UIImage *)addText:(UIImage *)img text:(NSString *)text1{

    int w = img.size.width;

    int h = img.size.height

    //lon = h - lon;

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    CGContextRef context = CGBitmapContextCreate(NULL, w, h, 84 * w, colorSpace,kCGImageAlphaPremultipliedFirst);

    

    CGContextDrawImage(context, CGRectMake(00, w, h), img.CGImage);

    CGContextSetRGBFillColor(context, 0.00.01.01);

    char* text= (char *)[text1 cStringUsingEncoding:NSASCIIStringEncoding];// "05/05/09";

    CGContextSelectFont(context, "Arial"18kCGEncodingMacRoman);

    CGContextSetTextDrawingMode(context, kCGTextFill);

    CGContextSetRGBFillColor(context, 2552552551);

 

    //rotate text

    CGContextSetTextMatrix(context, CGAffineTransformMakeRotation( -M_PI/4 ));

    CGContextShowTextAtPoint(context, 452, text, strlen(text));

    CGImageRef imageMasked = CGBitmapContextCreateImage(context);

    CGContextRelease(context);

    CGColorSpaceRelease(colorSpace);

    return [UIImage imageWithCGImage:imageMasked];

}


Tags: uiimage

General

Draw on UIImage

By Alex4. 五月 2009 13:15

-(UIImage *)addCircle:(UIImage *)img radius:(CGFloat)radius latCon:(CGFloat)lat lonCon:(CGFloat)lon{

    int w = img.size.width;

    int h = img.size.height; 

    lon = h - lon;

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    CGContextRef context = CGBitmapContextCreate(NULL, w, h, 84 * w, colorSpace, kCGImageAlphaPremultipliedFirst);

    

//draw the circle

CGContextDrawImage(context, CGRectMake(00, w, h), img.CGImage);

CGRect leftOval = {lat- radius/2, lon - radius/2, radius, radius};

CGContextSetRGBFillColor(context, 0.00.01.00.3);

CGContextAddEllipseInRect(context, leftOval);

CGContextFillPath(context);

    CGImageRef imageMasked = CGBitmapContextCreateImage(context);

    CGContextRelease(context);

    CGColorSpaceRelease(colorSpace);

    return [UIImage imageWithCGImage:imageMasked];

}

Tags: uiimage, uiimage, cgimageref, cgimageref

General | General

Add image to sqlite3

By Alex2. 五月 2009 15:46

-(BOOL)addImage:(UIImage *)imageItem{

int pegId = [self getNewjm_pegID];

if (!dbOpen) {

[self openDb];

}

NSString *insertProgrammeSql = @"INSERT INTO images (image) VALUES (?)";

sqlite3_stmt *statement;

if (sqlite3_prepare_v2(database, [insertProgrammeSql cStringUsingEncoding:NSUTF8StringEncoding], -1, &statement, NULL) == SQLITE_OK) {

NSData *imageData = UIImagePNGRepresentation(pegItem.albumCover);

sqlite3_bind_blob(statement, 1, [imageData bytes], [imageData length], SQLITE_TRANSIENT);

sqlite3_step(statement);

}

[self closeDb];

return YES;

}

 

-(UIImage *)getImage{

if (!dbOpen) {

[self openDb];

}

UIImage *image = nil;

NSMutableArray *pegs = [[NSMutableArray allocinit];

NSString *selectSql = @"SELECT image FROM images";

sqlite3_stmt *statement;

if (sqlite3_prepare_v2(database, [selectSql cStringUsingEncoding:NSUTF8StringEncoding], -1, &statement,NULL) == SQLITE_OK) {

int length = sqlite3_column_bytes(statement, 0);

NSData *imageData = [NSData dataWithBytes:sqlite3_column_blob(statement, 0 )length:length];

UIImage *image = [UIImage imageWithData:imageData];

[pegs addObject:pegItem];

}

}

sqlite3_finalize(statement);

[self closeDb];

return image;

}

Tags: uiimage, uiimage, uiimage, uiimage, uiimage, uiimage, uiimage, sqlite3, sqlite3, sqlite3, sqlite3, uiimage,sqlite3, sqlite3, sqlite3, sqlite3

原创粉丝点击