在我的应用程序中,要求是从照片库或相机中抓取图像,如果用户想要裁剪图像,那么他应该可以做到但我不知道如何裁剪从相机拍摄的图像或照片库。
如果你有什么想法,那就分享吧......
答案 0 :(得分:0)
您可以使用UIGraphicsGetImageFromCurrentImageContext
裁剪图像我们的想法是使用CoreGraphics将图像绘制到裁剪的图形上下文,然后将当前上下文导出到图像。
UIGraphicsGetImageFromCurrentImageContext返回基于的图像 当前基于位图的图形上下文的内容。
返回包含当前内容的图像对象 位图图形上下文。
尝试类似的东西:
- (UIImage*)imageCrop:(UIImage *)imageToCrop toRect:(CGRect)rect
{
UIGraphicsBeginImageContext(rect.size);
CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGRect clippedRect = CGRectMake(0, 0, rect.size.width, rect.size.height);
CGContextClipToRect( currentContext, clippedRect);
CGRect drawRect = CGRectMake(rect.origin.x, rect.origin.y, imageToCrop.size.width, imageToCrop.size.height);
CGContextDrawImage(currentContext, drawRect, imageToCrop.CGImage);
UIImage *cropped = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return cropped;
}
希望这有帮助, 文森特
答案 1 :(得分:0)
我在Mac上做过同样的事情,可能你可以尝试一下,
+(NSImage *)BreakImageForFirstImage:(NSImage *)pSrcImage Size:(NSSize)imageSize{
/*
Create a new NSImage with the size of the subrect, lock focus on it, draw the sub rect of the source image into
the new, locked image using -drawInRect:fromRect:operation:fraction: ("fromRect:" should be the subrect of the
source; the first rect argument will be a rect of the same size as the subrect, with a 0,0 origin), then unlock
focus from the new image. Done. Because of all that's involved in this, it might still be more efficient just to
load individual images
*/
NSSize srcSize = [pSrcImage size];
int subImageHeight = imageSize.height;
int subImageWidth = imageSize.width;
NSRect subImageRect = NSMakeRect(0,0,imageSize.width,imageSize.height);
NSRect tempRect = NSMakeRect(0,0,imageSize.width,imageSize.height);
//NSMutableArray *pImageArray = [[NSMutableArray alloc]initWithCapacity:noOfImages];
NSImage *pTempImage = nil;
int rowId =0;
int colId = 0;
//for(;rowId<noOfRow;rowId++)
{
pTempImage = [[NSImage alloc]initWithSize:imageSize];
tempRect = NSMakeRect(rowId*subImageWidth,colId*subImageHeight,subImageWidth,subImageHeight);
[pTempImage lockFocus];
[pSrcImage drawInRect:subImageRect fromRect:tempRect operation:NSCompositeSourceOver fraction:1.0];
[pTempImage unlockFocus];
//[pImageArray insertObject:pTempImage atIndex:rowId];
}
return pTempImage;
/* */
}
可能在某些地方,您可能需要将NS转换为UI以使其在iOS上运行