1

有什么方法可以让用户将图像上传到应用程序中,例如一张 50X150 像素的图像,然后我可以将其分成 3 张 50x50 像素的图像?

如果是这样,有人可以帮我选择某些像素并将其分成几张图像吗?

谢谢!

4

3 回答 3

2

使用此代码...

// 在下面的方法中 inRect:(CGRect)rect >>> 这个矩形应该是 50x50 或者你可以根据你的要求定义..

- (UIImage *)imageFromImage:(UIImage *)image inRect:(CGRect)rect {

CGImageRef sourceImageRef = [image CGImage];  
CGImageRef newImageRef = CGImageCreateWithImageInRect(sourceImageRef, rect);  
UIImage *newImage = [UIImage imageWithCGImage:newImageRef scale:1.0 orientation:image.imageOrientation];
CGImageRelease(newImageRef);
return newImage;
} 

有关更多信息,请访问此参考..

希望,这会帮助你......享受

于 2012-04-21T04:20:34.200 回答
1

在 UIImage 上定义一个类别,为您提供一个很好的裁剪方法:

- (UIImage *)cropImageInRect:(CGRect)cropRect
{
    CGImageRef image = CGImageCreateWithImageInRect(self.CGImage,cropRect);
    UIImage *croppedImage = [UIImage imageWithCGImage:image];
    CGImageRelease(image);
    return croppedImage;
}

现在有了这个类别,您可以轻松地做您想做的事:

UIImage *original = ...;
UIImage left = [original cropImageInRect:CGRectMake(0.0, 0.0, 50.0, 50.0)];
UIImage center = [original cropImageInRect:CGRectMake(0.0, 50.0, 50.0, 50.0)];
UIImage right = [original cropImageInRect:CGRectMake(0.0, 100.0, 50.0, 50.0)];
于 2012-04-21T04:57:17.720 回答
1

我也需要这个。添加到 UIImage 上的 utils 类别方法:

// UIImage+Utls.h

@interface UIImage (UIImage_Utls)

- (UIImage *)subimageInRect:(CGRect)rect;
- (NSArray *)subimagesHorizontally:(NSInteger)count;

@end

// UIImage+Utls.m

#import "UIImage+Utls.h"

@implementation UIImage (UIImage_Utls)

- (UIImage *)subimageInRect:(CGRect)rect {

    CGImageRef imageRef = CGImageCreateWithImageInRect([self CGImage], rect);
    UIImage *answer = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);
    return answer;
}

- (NSArray *)subimagesHorizontally:(NSInteger)count {

    NSMutableArray *answer = [NSMutableArray arrayWithCapacity:count];
    CGFloat width = self.size.width / count;
    CGRect rect = CGRectMake(0.0, 0.0, width, self.size.height);

    for (int i=0; i<count; i++) {
        [answer addObject:[self subimageInRect:rect]];
        rect = CGRectOffset(rect, width, 0.0);
    }
    return [NSArray arrayWithArray:answer];
}

@end
于 2012-04-21T05:00:01.670 回答