- (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);
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以下html
- 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,並可 根據本身的實際使用場景,設置壓縮係數,進一步下降圖片數據量大小。less
- 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具體codeide
- #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