2011-10-31 6 views
4

this 질문과 같은 질문을하고 싶습니다. 내 앱에서 FaceBook에서 이미지 자르기처럼 이미지 자르기를하고 싶습니다. 좋은 자습서 또는 샘플 코드 링크를 통해 나를 안내 할 수 있습니다. 내가 제공 한 링크는 나의 요구 사항을 완벽하게 기술 할 것입니다.iPhone에서 이미지 자르기 방법

답변

4

모든 속성을 사용하여 새 이미지를 만들 수 있습니다. 여기 내 기능이있다, 마녀가 그렇게 해. 새로운 이미지의 매개 변수를 사용해야합니다. 제 경우에는 이미지가 잘리지 않고 그냥 원래의 위치에서 다른 곳으로 픽셀을 옮겨서 약간의 효과를냅니다. 다른 높이와 폭으로 새로운 이미지를 초기화하는 경우에, 당신은 새로운 하나에, 당신이 필요로 이전 이미지의 픽셀의 범위에서 복사 할 수 있습니다 :

-(UIImage *)Color:(UIImage *)img 
{ 
    int R; 
    float m_width = img.size.width; 
    float m_height = img.size.height; 
    if (m_width>m_height) R = m_height*0.9; 
    else R = m_width*0.9; 
    int m_wint = (int)m_width;  //later, we will need this parameters in float and int. you may just use "(int)" and "(float)" before variables later, and do not implement another ones 
    int m_hint = (int)m_height; 

    CGRect imageRect; 
    //cheking image orientation. we will work with image pixel-by-pixel, so we need to make top side at the top. 
    if(img.imageOrientation==UIImageOrientationUp 
     || img.imageOrientation==UIImageOrientationDown) 
    { 
     imageRect = CGRectMake(0, 0, m_wint, m_hint); 
    } 
    else 
    { 
     imageRect = CGRectMake(0, 0, m_hint, m_wint); 
    } 
    uint32_t *rgbImage = (uint32_t *) malloc(m_wint * m_hint * sizeof(uint32_t)); 
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
    CGContextRef context = CGBitmapContextCreate(rgbImage, m_wint, m_hint, 8, m_wint *sizeof(uint32_t), colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipLast); 
    CGContextSetInterpolationQuality(context, kCGInterpolationHigh); 
    CGContextSetShouldAntialias(context, NO); 
    CGContextTranslateCTM(context, 0, m_hint); 
    CGContextScaleCTM(context, 1.0, -1.0); 
    switch (img.imageOrientation) { 
     case UIImageOrientationRight: 
     { 
      CGContextRotateCTM(context, M_PI/2); 
      CGContextTranslateCTM(context, 0, -m_wint);    
     }break; 
     case UIImageOrientationLeft: 
     { 
      CGContextRotateCTM(context, - M_PI/2); 
      CGContextTranslateCTM(context, -m_hint, 0);    
     }break; 
     case UIImageOrientationUp: 
     { 
      CGContextTranslateCTM(context, m_wint, m_hint); 
      CGContextRotateCTM(context, M_PI); 
     } 
     default: 
      break; 
    } 

    CGContextDrawImage(context, imageRect, img.CGImage); 
    CGContextRelease(context); 
    CGColorSpaceRelease(colorSpace); 

    //here is new image. you can change m_wint and m_hint as you whant 
    uint8_t *result = (uint8_t *) calloc(m_wint * m_hint * sizeof(uint32_t), 1); 
    for(int y = 0; y < m_hint; y++) //new m_hint here 
    { 
     float fy=y; 
     double yy = (m_height*( asinf(m_height/(2*R))-asin(((m_height/2)-fy)/R) ))/
     (2*asin(m_height/(2*R))); // (xx, yy) - coordinates of pixel of OLD image 
     for(int x = 0; x < m_wint; x++) //new m_wint here 
     { 
      float fx=x; 
      double xx = (m_width*( asin(m_width/(2*R))-asin(((m_width/2)-fx)/R) ))/
      (2*asin(m_width/(2*R))); 
      uint32_t rgbPixel=rgbImage[(int)yy * m_wint + (int)xx]; 
      int intRedSource = (rgbPixel>>24)&255; 
      int intGreenSource = (rgbPixel>>16)&255; 
      int intBlueSource = (rgbPixel>>8)&255; 
      result[(y * (int)m_wint + x) * 4] = 0; 
      result[(y * (int)m_wint + x) * 4 + 1] = intBlueSource; 
      result[(y * (int)m_wint + x) * 4 + 2] = intGreenSource; 
      result[(y * (int)m_wint + x) * 4 + 3] = intRedSource; 

     } 
    } 

    free(rgbImage); 

    colorSpace = CGColorSpaceCreateDeviceRGB(); 
    context = CGBitmapContextCreate(result, m_wint, m_hint, 8, m_wint * sizeof(uint32_t), colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipLast ); //new m_wint and m_hint as well 


    CGImageRef image1 = CGBitmapContextCreateImage(context); 
    CGContextRelease(context); 
    CGColorSpaceRelease(colorSpace); 
    UIImage *resultUIImage = [UIImage imageWithCGImage:image1]; 
    CGImageRelease(image1); 


    @try { 
     free(result); 
    } 
    @catch (NSException * e) { 
     NSLog(@"proc. Exception: %@", e); 
    } 

    return resultUIImage; 
} 
+0

감사 센티넬 응답하지만 저가 나는 코드'CGRect cropRect = CGRectMake (100, 100, 200, 200)의 라인 뭐하는 거지 내 이미지를 자르 할 질문은하지 않았다 나는 것은 ; \t CGImageRef imageRef = CGImageCreateWithImageInRect (ImageREF.CGImage, cropRect);하지만이 자르기는 하드 코딩되어 있습니다. 사용자가 [link]에 표시된 이미지 링크의 Rect를 조정하기를 원합니다 (http://img192.imageshack.us/ img192/8930/customcropbox.jpg) –

+0

그래서 ... 사용자 인터페이스를 만들고, 이미지를 자르려면 rect를 선택해야합니까? 이것은 쉽습니다. 단지 움직이는 부분에 터치를 적용하십시오. 알파 0.7이있는 4 개의 블레이크 막대는 자르기 될 영역을 음소거합니다. 당신은 2 개의 터치를 다룰 수 있습니다 : 좌표에 따라, 그들이 ractangle의 대각선의 다른면에 있다는 것을 안다면, 4 개의 어두운 막대의 크기를 쉽게 계산할 수 있습니다. 매 움직임마다 tham을 변경하십시오. 또는 한 번의 터치로 4 칸 (파란색 원)이 필요합니다. 궤도를 건 드리면 바퀴가 움직입니다. – SentineL

+0

고마워요. SentineL이 응답하고 길을 안내 해줍니다. –

3

CGRect rectImage = CGRectMake (을 P1.x, p1.y , p2.x-p1.x, p4.y-p1.y);

//Create bitmap image from original image data, 
//using rectangle to specify desired crop area 

CGImageRef imageRef = CGImageCreateWithImageInRect([imageForCropping CGImage], rectImage); 
UIImage *croppedImage = [UIImage imageWithCGImage:imageRef]; 
imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(p1.x, p1.y,p2.x-p1.x p4.y-p1.y)]; 
imageView1.image = croppedImage; 
[self.view addSubview:imageView1]; 
CGImageRelease(imageRef); 
+1

여기 image 포획은 자르기를 원하는 이미지입니다. 자른 부분을 자르려고합니다. –

+0

어떻게 생각하세요? 이미지가 회전 할 때 (예 : 아이폰 카메라에서 찍은 경우)이 작업을 수행합니까? –

+0

나는 이미지 자르기를하고 싶을 때 이미지를 회전시킬 때 !!!! –