我现在需要在iphone上实现对一张图片进行裁剪的功能,就是可以选择图片的任何区域进行裁剪,但由于本人对iphone的开发还在初级阶段,希望有大虾帮忙指点一下,谢谢!

解决方案 »

  1.   

    //myImageRect 图片的中一个矩形区域
      
      //image 图片
       CGRect myImageRect = CGRectMake(x, y, subWidth, subHeight);
       CGImageRef imageRef = image.CGImage;
        CGImageRef subImageRef = CGImageCreateWithImageInRect(imageRef, myImageRect);
        CGSize size;
        size.width = subWidth;
        size.height = subHeight;
        UIGraphicsBeginImageContext(size);
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextDrawImage(context, myImageRect, subImageRef);
        UIImage* imageCliped = [UIImage imageWithCGImage:subImageRef];
        UIGraphicsEndImageContext();
      

  2.   

    UIGraphicsBeginImageContext(CGSizeMake(IMAGE_SIZE, IMAGE_SIZE)); 
    CGContextRef context=UIGraphicsGetCurrentContext();//获取当前上下文
    CGContextTranslateCTM(context, 0.0, image.size.height);//将图片坐标转换成quartz的坐标
    CGContextScaleCTM(context, 1.0, -1.0);//将图片坐标转换成quartz的坐标
    CGContextDrawImage(context, CGRectMake(-subImageLocationX, i*IMAGE_SIZE, image.size.width, image.size.height), [image CGImage]); //截取图片画在上下文上,注意x轴坐标是负的
    UIImage *subImage= UIGraphicsGetImageFromCurrentImageContext();//从上下文上获取图片
    UIGraphicsEndImageContext();这样也能实现,而且不会造成不可回收的内存