원래는 인식기로 crop하지만 그냥 특정 좌표만큼 짜르고 싶을 때 사용
#define DEVICE_HEIGHT ([[UIScreen mainScreen] bounds].size.height)
#define DEVICE_WIDTH ([[UIScreen mainScreen] bounds].size.width)
/**
이미지를 rect만큼 잘라주는 함수
@param image 원본 이미지
@param rect 좌표 정보
@return UIImage 잘라진 이미지
*/
- (UIImage*)imageCrop : (UIImage*)image withGuideRect : (CGRect) guideRect
{
//guideRect 크기 계산
NSLog(@"image width = %f, height = %f",image.size.width, image.size.height);
float xRatio, yRatio;
float bigValue, smallValue;
bigValue = (DEVICE_HEIGHT>DEVICE_WIDTH)?DEVICE_HEIGHT:DEVICE_WIDTH;
smallValue = (DEVICE_HEIGHT>DEVICE_WIDTH)?DEVICE_WIDTH:DEVICE_HEIGHT;
xRatio = (float)image.size.width / smallValue;
yRatio = (float)image.size.height / bigValue;
NSLog(@"%f,%f",xRatio, yRatio);
CGRect rect = CGRectMake(guideRect.origin.x*xRatio, guideRect.origin.y*yRatio, guideRect.size.width*xRatio, guideRect.size.height*yRatio);
//이미지 크랍
NSLog(@"cropedImage width = %f, height = %f",rect.size.width, rect.size.height);
NSData* rgbpixelData = (NSData*) CGDataProviderCopyData(CGImageGetDataProvider(image.CGImage));
unsigned char* rgbpixelBytes = (unsigned char *)[rgbpixelData bytes];
int bpp = 4;
int oriW = image.size.width;
//int oriH = image.size.height;
int newW = rect.size.width;
int newH = rect.size.height;
int newSize = newW * newH * bpp;
int newX = rect.origin.x;
int newY = rect.origin.y;
unsigned char * pSrc = rgbpixelBytes;
unsigned char *pDst = new unsigned char[newSize];
memset (pDst, 0, newSize * sizeof(unsigned char));
int y = 0;
for (y = newY; y < newY + newH; ++y)
{
unsigned char *pD = pDst + ((y - newY) * newW * bpp);
unsigned char *pS = pSrc + (y * oriW * bpp) + newX*bpp;
memcpy (pD, pS, newW * bpp);
}
NSData *skewrgbImageData = [[NSData alloc] initWithBytes:pDst length:(newSize)];
delete[] pDst;
pDst= NULL;
CGDataProviderRef imgrgbDataProvider = CGDataProviderCreateWithCFData((CFDataRef)skewrgbImageData);
int bitsPerComponent = 8;
int bitsPerPixel = 32;
int bytesPerRow = 4 * newW;
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(image.CGImage);
if (bitmapInfo & kCGBitmapByteOrder32Big)
{
bitmapInfo= kCGBitmapByteOrder32Big | kCGImageAlphaNone;
}
else
bitmapInfo= kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipFirst;
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
CGImageRef imageRef = CGImageCreate(newW, newH, bitsPerComponent, bitsPerPixel, bytesPerRow,colorSpaceRef, bitmapInfo, imgrgbDataProvider, NULL, false, renderingIntent);
UIImage *cropImage = [UIImage imageWithCGImage:imageRef];
CFRelease(imageRef);
CGColorSpaceRelease(colorSpaceRef);
CGDataProviderRelease(imgrgbDataProvider);
[skewrgbImageData release];
skewrgbImageData = nil;
CFRelease(rgbpixelData);
return cropImage;
}
'프로그래밍 > iOS' 카테고리의 다른 글
[iOS] 엔터프라이즈 배포 (0) | 2015.09.30 |
---|---|
[iOS] UIImage Resize (0) | 2015.09.14 |
[iOS] UI 회전시키기 (0) | 2015.08.17 |
[ios] device orientation에 따라 이미지를 회전시키는 함수 (0) | 2015.08.17 |
[iOS] 시간 측정하기 (0) | 2015.08.10 |