Проблема с обрезкой UIImage с помощью CGContext?

Я разрабатываю простое приложение UIA, в котором я хочу обрезать UIImage (в формате .jpg) с помощью CGContext. Разработанный код до сих пор выглядит следующим образом:

CGImageRef graphicOriginalImage = [originalImage.image CGImage];

UIGraphicsBeginImageContext(originalImage.image.size);

CGContextRef ctx = UIGraphicsGetCurrentContext();
CGBitmapContextCreateImage(graphicOriginalImage);

CGFloat fltW = originalImage.image.size.width;
CGFloat fltH = originalImage.image.size.height;
CGFloat X = round(fltW/4); 
CGFloat Y =round(fltH/4);
CGFloat width = round(X + (fltW/2));
CGFloat height = round(Y + (fltH/2));   

CGContextTranslateCTM(ctx, 0, image.size.height);
CGContextScaleCTM(ctx, 1.0, -1.0);
CGRect rect = CGRectMake(X,Y ,width ,height); 
CGContextDrawImage(ctx, rect, graphicOriginalImage);

croppedImage = UIGraphicsGetImageFromCurrentImageContext();

return croppedImage;

} Приведенный выше код работает нормально, но не может обрезать изображение. Память исходного изображения и память обрезанного изображения будут одинаковыми (равными памяти исходного изображения). Приведенный выше код подходит для обрезки изображения ??????????????????


person Tirth    schedule 16.04.2010    source источник


Ответы (2)


Контекст, который вы создаете для рисования изображения, имеет тот же размер, что и исходное изображение. Вот почему они имеют одинаковый размер.

Если вы не хотите изобретать велосипед, взгляните на TouchCode. проект в Google Code. Вы найдете категории UIImage, которые выполняют свою работу (см. UIImage_ThumbnailExtensions.m).

person Laurent Etiemble    schedule 16.04.2010
comment
На данный момент скачивание недоступно. Но вы можете использовать Mercurial для клонирования репозитория. - person Laurent Etiemble; 17.04.2010
comment
У вас есть идеи об обрезке изображения с использованием указателя пикселей на anf с использованием методов CGContext? - person Tirth; 17.04.2010

Вот хороший способ обрезать изображение до CGRect:


- (UIImage*)imageByCropping:(UIImage *)imageToCrop toRect:(CGRect)rect
{
   //create a context to do our clipping in
   UIGraphicsBeginImageContext(rect.size);
   CGContextRef currentContext = UIGraphicsGetCurrentContext();

   //create a rect with the size we want to crop the image to
   //the X and Y here are zero so we start at the beginning of our
   //newly created context
   CGRect clippedRect = CGRectMake(0, 0, rect.size.width, rect.size.height);
   CGContextClipToRect( currentContext, clippedRect);

   //create a rect equivalent to the full size of the image
   //offset the rect by the X and Y we want to start the crop
   //from in order to cut off anything before them
   CGRect drawRect = CGRectMake(rect.origin.x * -1,
                                rect.origin.y * -1,
                                imageToCrop.size.width,
                                imageToCrop.size.height);

   //draw the image to our clipped context using our offset rect
   CGContextDrawImage(currentContext, drawRect, imageToCrop.CGImage);

   //pull the image from our cropped context
   UIImage *cropped = UIGraphicsGetImageFromCurrentImageContext();

   //pop the context to get back to the default
   UIGraphicsEndImageContext();

   //Note: this is autoreleased
   return cropped;
}

Или по-другому:


- (UIImage *)imageByCropping:(UIImage *)imageToCrop toRect:(CGRect)rect
 {
  CGImageRef imageRef = CGImageCreateWithImageInRect([imageToCrop CGImage], rect);

  UIImage *cropped = [UIImage imageWithCGImage:imageRef];
  CGImageRelease(imageRef);


  return cropped;

}

С http://www.hive05.com/2008/11/crop-an-image-using-the-iphone-sdk/.

person cduck    schedule 12.12.2010