2013-11-06 3 views
1

У меня есть PNG, загруженный в UIImage. Я хочу получить часть изображения на основе пути (т. Е. Он может быть не прямоугольником). Скажем, это может быть какая-то форма с дугами и т. Д. Как путь рисования.Проводка от UIImage

Что было бы самым простым способом сделать это?

Спасибо.

ответ

1

Самый простой способ добавить категорию в UIImage с помощью метода наблюдения:

-(UIImage *)scaleToRect:(CGRect)rect{ 
// Create a bitmap graphics context 
// This will also set it as the current context 
UIGraphicsBeginImageContext(size); 

// Draw the scaled image in the current context 
[self drawInRect:rect]; 

// Create a new image from current context 
UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext(); 

// Pop the current context from the stack 
UIGraphicsEndImageContext(); 

// Return our new scaled image 
return scaledImage; 

}

2

Я не запускать это, поэтому он не может быть совершенным, но это должно дать вам представление о том ,

UIImage *imageToClip = //get your image somehow 
CGPathRef yourPath = //get your path somehow 
CGImageRef imageRef = [imageToClip CGImage]; 

size_t width = CGImageGetWidth(imageRef);   
size_t height = CGImageGetHeight(imageRef); 
CGContextRef context = CGBitmapContextCreate(NULL, width, height, 8, 0, CGImageGetColorSpace(imageRef), kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedFirst); 
CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef); 
CGContextAddPath(context, yourPath); 
CGContextClip(context); 

CGImageRef clippedImageRef = CGBitmapContextCreateImage(context); 
UIImage *clippedImage = [UIImage imageWithCGImage:clippedImageRef];//your final, masked image 

CGImageRelease(clippedImageRef); 
CGContextRelease(context); 
Смежные вопросы