2012-03-06 76 views
0

在我的应用程序中,需求是从照片库或相机抓取图像,如果用户想裁剪图像,那么他应该能够做到这一点,但我不知道如何裁剪从相机或照片库中获取的图像。iPhone:如何在ios5中裁剪图像

,如果你有任何想法,然后分享它...

回答

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

这会使我的图像颠倒。 – Shailesh 2012-10-23 13:40:07

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