Core graphics UIImage

http://iphonesdksnippets.com/?tag=/uiimage


Convert image to/from text (Base64)

by alex 14. March 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)

Currently rated 5.0 by 4 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

General

Create screenshot in runtime (inside UIView)

by alex 7. November 2009 10:35

UIGraphicsBeginImageContext(self.bounds.size);

[self.layer renderInContext:UIGraphicsGetCurrentContext()];

UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

Currently rated 5.0 by 4 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: ,

General

Get pixel information from UIImage

by alex 4. October 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(0, 0, width, height), image);

CGContextRelease(context1);

 

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

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


Currently rated 5.0 by 2 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

General

Convert UIImage to NSData

by alex 2. June 2009 14:34

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

NSData *dataObj = UIImageJPEGRepresentation(img, 1.0);

Currently rated 4.0 by 11 people

  • Currently 4/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: ,

General

Save image to image library

by alex 12. May 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 alloc] initWithTitle:@"Saved." message:str delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];

    [alert show];

}


Currently rated 4.3 by 6 people

  • Currently 4.333333/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

General

Download image from HTTP server (faster)

by alex 9. May 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:[NSURL URLWithString:@"http://www.google.com/intl/en_ALL/images/logo.gif"]];

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

UIImage *image = [UIImage imageWithData:imageData];


Currently rated 4.1 by 7 people

  • Currently 4.142857/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , ,

General

Resize image and keep aspect ratio

by alex 6. May 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, 8, 4 * 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(0, 0, width, height), imageRef);

CGImageRef ref = CGBitmapContextCreateImage(bitmap);

UIImage *result = [UIImage imageWithCGImage:ref];

CGContextRelease(bitmap);

CGImageRelease(ref);

return result;

}

Currently rated 5.0 by 4 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

General

Add text to image (UIImage)

by alex 5. May 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, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);

    

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

    CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1);

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

    CGContextSelectFont(context, "Arial", 18, kCGEncodingMacRoman);

    CGContextSetTextDrawingMode(context, kCGTextFill);

    CGContextSetRGBFillColor(context, 255, 255, 255, 1);

 

    //rotate text

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

    CGContextShowTextAtPoint(context, 4, 52, text, strlen(text));

    CGImageRef imageMasked = CGBitmapContextCreateImage(context);

    CGContextRelease(context);

    CGColorSpaceRelease(colorSpace);

    return [UIImage imageWithCGImage:imageMasked];

}


Currently rated 4.1 by 12 people

  • Currently 4.083333/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

General

Draw on UIImage

by alex 4. May 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, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);

    

//draw the circle

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

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

CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 0.3);

CGContextAddEllipseInRect(context, leftOval);

CGContextFillPath(context);

    CGImageRef imageMasked = CGBitmapContextCreateImage(context);

    CGContextRelease(context);

    CGColorSpaceRelease(colorSpace);

    return [UIImage imageWithCGImage:imageMasked];

}

Currently rated 2.0 by 2 people

  • Currently 2/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: ,

General

Add image to sqlite3

by alex 2. May 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 alloc] init];

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;

}

Currently rated 5.0 by 2 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: ,

General



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值