UIColor* color = nil;
CGImageRef inImage = self.image.CGImage;
// Create off screen bitmap context to draw the image into. Format ARGB is 4 bytes for each pixel: Alpa, Red, Green, Blue
CGContextRef cgctx = [self createARGBBitmapContextFromImage:inImage];
if (cgctx == NULL) { return nil; }
size_t w = CGImageGetWidth(inImage);
size_t h = CGImageGetHeight(inImage);
CGRect rect = {{ 0, 0},{w,h}};
// Draw the image to the bitmap context. Once we draw, the memory
// allocated for the context for rendering will then contain the
// raw image data in the specified color space.
CGContextDrawImage(cgctx, rect, inImage);
// Now we can get a pointer to the image data associated with the bitmap
// context.
unsigned char* data = CGBitmapContextGetData (cgctx);
if (data != NULL) {
//offset locates the pixel in the data from x,y.
//4 for 4 bytes of data per pixel, w is width of one row of data.
@try {
int offset = 4*((w*round(point.y))+round(point.x));
NSLog( @"offset: %d", offset);
int alpha = data[offset];
int red = data[offset+ 1];
int green = data[offset+ 2];
int blue = data[offset+ 3];
NSLog( @"offset: %i colors: RGB A %i %i %i %i",offset,red,green,blue,alpha);
color = [UIColor colorWithRed:(red/ 255.0f) green:(green/ 255.0f) blue:(blue/ 255.0f) alpha:(alpha/ 255.0f)];
}
@catch (NSException * e) {
NSLog( @"%@",[e reason]);
}
@finally {
}
}
// When finished, release the context
CGContextRelease(cgctx);
// Free image data memory for the context
if (data) { free(data); }
return color;
}
CGContextRef context = NULL;
CGColorSpaceRef colorSpace;
void * bitmapData;
int bitmapByteCount;
int bitmapBytesPerRow;
// Get image width, height. We'll use the entire image.
size_t pixelsWide = CGImageGetWidth(inImage);
size_t pixelsHigh = CGImageGetHeight(inImage);
// Declare the number of bytes per row. Each pixel in the bitmap in this
// example is represented by 4 bytes; 8 bits each of red, green, blue, and
// alpha.
bitmapBytesPerRow = (pixelsWide * 4);
bitmapByteCount = (bitmapBytesPerRow * pixelsHigh);
// Use the generic RGB color space.
colorSpace = CGColorSpaceCreateDeviceRGB();
if (colorSpace == NULL)
{
fprintf(stderr, "Error allocating color space \n ");
return NULL;
}
// Allocate memory for image data. This is the destination in memory
// where any drawing to the bitmap context will be rendered.
bitmapData = malloc( bitmapByteCount );
if (bitmapData == NULL)
{
fprintf (stderr, "Memory not allocated!");
CGColorSpaceRelease( colorSpace );
return NULL;
}
// Create the bitmap context. We want pre-multiplied ARGB, 8-bits
// per component. Regardless of what the source image format is
// (CMYK, Grayscale, and so on) it will be converted over to the format
// specified here by CGBitmapContextCreate.
context = CGBitmapContextCreate (bitmapData,
pixelsWide,
pixelsHigh,
8, // bits per component
bitmapBytesPerRow,
colorSpace,
kCGImageAlphaPremultipliedFirst);
if (context == NULL)
{
free (bitmapData);
fprintf (stderr, "Context not created!");
}
// Make sure and release colorspace before returning
CGColorSpaceRelease( colorSpace );
return context;
}
- (UIColor *) colorOfPoint:(CGPoint)point { unsigned char pixel[4] = {0}; CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); CGContextRef context = CGBitmapContextCreate(pixel, 1, 1, 8, 4, colorSpace, kCGImageAlphaPremultipliedLast); CGContextTranslateCTM(context, -point.x, -point.y); [self.layer renderInContext:context]; CGContextRelease(context); CGColorSpaceRelease(colorSpace); //NSLog(@"pixel: %d %d %d %d", pixel[0], pixel[1], pixel[2], pixel[3]); UIColor *color = [UIColor colorWithRed:pixel[0]/255.0 green:pixel[1]/255.0 blue:pixel[2]/255.0 alpha:pixel[3]/255.0]; return color; }
//圖片壓縮
iOS自帶的提供了一個API如下
- NSData *UIImageJPEGRepresentation(UIImage *image, CGFloat compressionQuality);
在Iphone上有兩種讀取圖片數據的簡單方法: UIImageJPEGRepresentation和UIImagePNGRepresentation. UIImageJPEGRepresentation函數需要兩個參數:圖片的引用和壓縮系數.而UIImagePNGRepresentation只需 要圖片引用作為參數.通過在實際使用過程中,比較發現: UIImagePNGRepresentation(UIImage* image) 要比UIImageJPEGRepresentation(UIImage* image, 1.0) 返回的圖片數據量大很多.譬如,同樣是讀取攝像頭拍攝的同樣景色的照片, UIImagePNGRepresentation()返回的數據量大小為199K ,而 UIImageJPEGRepresentation(UIImage* image, 1.0)返回的數據量大小只為140KB,比前者少了50多KB.如果對圖片的清晰度要求不高,還可以通過設置 UIImageJPEGRepresentation函數的第二個參數,大幅度降低圖片數據量.譬如,剛才拍攝的圖片, 通過調用UIImageJPEGRepresentation(UIImage* image, 1.0)讀取數據時,返回的數據大小為140KB,但更改壓縮系數后,通過調用UIImageJPEGRepresentation(UIImage* image, 0.5)讀取數據時,返回的數據大小只有11KB多,大大壓縮了圖片的數據量 ,而且從視角角度看,圖片的質量並沒有明顯的降低.因此,在讀取圖片數據內容時,建議優先使用UIImageJPEGRepresentation,並可 根據自己的實際使用場景,設置壓縮系數,進一步降低圖片數據量大小。
- UIImage *imageNew = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
- imageNew = [self imageWithImage:imageNew scaledToSize:CGSizeMake(100, 100)];
- NSData *imageData = UIImageJPEGRepresentation(imageNew, 0.0001);
- m_selectImage = [UIImage imageWithData:imageData];
.h具體code
- #import <Foundation/Foundation.h>
- @interface UIImage (UIImageExt)
- - (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size;
- - (UIImage *)imageByScalingAndCroppingForSize:(CGSize)targetSize;
- @end
.m具體code
- #import "UIImageExt.h"
- @implementation UIImage (UIImageExt)
- - (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size{
- // 創建一個bitmap的context
- // 並把它設置成為當前正在使用的context
- UIGraphicsBeginImageContext(size);
- // 繪制改變大小的圖片
- [img drawInRect:CGRectMake(0, 0, size.width, size.height)];
- // 從當前context中創建一個改變大小后的圖片
- UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
- // 使當前的context出堆棧
- UIGraphicsEndImageContext();
- // 返回新的改變大小后的圖片
- return scaledImage;
- }
- - (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize
- {
- UIImage *sourceImage = self;
- UIImage *newImage = nil;
- CGSize imageSize = sourceImage.size;
- CGFloat width = imageSize.width;
- CGFloat height = imageSize.height;
- CGFloat targetWidth = targetSize.width;
- CGFloat targetHeight = targetSize.height;
- CGFloat scaleFactor = 0.0;
- CGFloat scaledWidth = targetWidth;
- CGFloat scaledHeight = targetHeight;
- CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
- if (CGSizeEqualToSize(imageSize, targetSize) == NO)
- {
- CGFloat widthFactor = targetWidth / width;
- CGFloat heightFactor = targetHeight / height;
- if (widthFactor > heightFactor)
- scaleFactor = widthFactor; // scale to fit height
- else
- scaleFactor = heightFactor; // scale to fit width
- scaledWidth = width * scaleFactor;
- scaledHeight = height * scaleFactor;
- // center the image
- if (widthFactor > heightFactor)
- {
- thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
- }
- else
- if (widthFactor < heightFactor)
- {
- thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
- }
- }
- UIGraphicsBeginImageContext(targetSize); // this will crop
- CGRect thumbnailRect = CGRectZero;
- thumbnailRect.origin = thumbnailPoint;
- thumbnailRect.size.width = scaledWidth;
- thumbnailRect.size.height = scaledHeight;
- [sourceImage drawInRect:thumbnailRect];
- newImage = UIGraphicsGetImageFromCurrentImageContext();
- if(newImage == nil)
- NSLog(@"could not scale image");
- //pop the context to get back to the default
- UIGraphicsEndImageContext();
- return newImage;
- }
- @end