0

从 REST 端点传输大图像后,我需要将图像分成许多较小的图像块。

初始图像(例如)1024x1024,存储在 NSData 中;我需要创建大小为 256x256 的子图像(在这种情况下,将有 16 个子图像)。

这将如何完成?(我还没有找到任何接近的文章,但我认为这一定是可能的,因为大多数图像编辑软件都支持图像裁剪。)

谢谢。

4

3 回答 3

1

这是我在某些项目中用来裁剪图像的功能。

- (UIImage *)cropImage:(UIImage *) image{
   CGRect rect = CGRectMake(0, 0, 256, 256);
   CGImageRef subImageRef = CGImageCreateWithImageInRect(image.CGImage, rect);
   CGRect smallBounds = CGRectMake(0, 0, CGImageGetWidth(subImageRef), CGImageGetHeight(subImageRef));

   UIGraphicsBeginImageContext(smallBounds.size);
   CGContextRef context = UIGraphicsGetCurrentContext();
   CGContextDrawImage(context, smallBounds, subImageRef);
   UIImage* smallImg = [UIImage imageWithCGImage:subImageRef];
   UIGraphicsEndImageContext();

   return smallImg;
}

我认为你可以从那里多次调用它来裁剪你的照片 16 次。

希望这可以帮助

于 2012-05-20T00:34:25.067 回答
1

originalImageView 是一个 IBOutlet ImageView。此图像将被裁剪。

#import <QuartzCore/QuartzCore.h>

为了更好地理解,每个切片周围的边界都需要这样做。

-(UIImage*)getCropImage:(CGRect)cropRect
{
CGImageRef image = CGImageCreateWithImageInRect([originalImageView.image CGImage],cropRect);
UIImage *cropedImage = [UIImage imageWithCGImage:image];
CGImageRelease(image);
return cropedImage;
}

-(void)prepareSlices:(uint)row:(uint)col
{
float flagX = originalImageView.image.size.width / originalImageView.frame.size.width;
float flagY = originalImageView.image.size.height / originalImageView.frame.size.height;

float _width    = originalImageView.frame.size.width / col;
float _height   = originalImageView.frame.size.height / row;

float _posX = 0.0;
float _posY = 0.0;

for (int i = 1; i <= row * col; i++) {

    UIImageView *croppedImageVeiw = [[UIImageView alloc] initWithFrame:CGRectMake(_posX, _posY, _width, _height)];
    UIImage *img = [self getCropImage:CGRectMake(_posX * flagX,_posY * flagY, _width * flagX, _height * flagY)];
    croppedImageVeiw.image = img;

    croppedImageVeiw.layer.borderColor = [[UIColor whiteColor] CGColor];
    croppedImageVeiw.layer.borderWidth = 1.0f;

    [self.view addSubview:croppedImageVeiw];
    [croppedImageVeiw release];

    _posX += _width;

    if (i % col == 0) {
        _posX = 0;
        _posY += _height;
    }

}

originalImageView.alpha = 0.0;

}

像这样称呼它:

[self prepareSlices:16 :16];
于 2012-05-20T05:07:46.787 回答
0
@interface UIImage (Sprite)
- (NSArray *)spritesWithSpriteSheetImage:(UIImage *)image spriteSize:(CGSize)size;
- (NSArray *)spritesWithSpriteSheetImage:(UIImage *)image inRange:(NSRange)range spriteSize:(CGSize)size;
@end




@implementation UIImage (Sprite)

-(NSArray *)spritesWithSpriteSheetImage:(UIImage *)image spriteSize:(CGSize)size {
    return [self spritesWithSpriteSheetImage:self inRange:NSMakeRange(0, lroundf(MAXFLOAT)) 
                              spriteSize:size];
}
-(NSArray *)spritesWithSpriteSheetImage:(UIImage *)image 
                            inRange:(NSRange)range 
                         spriteSize:(CGSize)size {
if (!image || CGSizeEqualToSize(size, CGSizeZero) || range.length == 0) 
    return nil;
NSLog(@"%i %i", range.location, range.length);
CGImageRef spriteSheet = [image CGImage];
NSMutableArray *tempArray = [[[NSMutableArray alloc] init] autorelease];

int width = CGImageGetWidth(spriteSheet);
int height = CGImageGetHeight(spriteSheet);

int maxI = width / size.width;
int startI = 0;
int startJ = 0;
int length = 0;
int startPosition = range.location;

// Extracting initial I & J values from range info
//
if (startPosition != 0) {
    for (int k=1; k<=maxI; k++) {
        int d = k * maxI;
        if (d/startPosition == 1) {
            startI = maxI - (d % startPosition);
            break;
        }
        else if (d/startPosition > 1) {
            startI = startPosition;
            break;
        }
        startJ++;
    }
}
int positionX = startI * size.width;
int positionY = startJ * size.height;
BOOL isReady = NO;
while (positionY < height) {
    while (positionX < width) {
        CGImageRef sprite = CGImageCreateWithImageInRect(spriteSheet, CGRectMake(positionX, positionY, size.width, size.height));
        [tempArray addObject:[UIImage imageWithCGImage:sprite]];
        CGImageRelease(sprite);
        length++;
        if (length == range.length) {
            isReady = YES;
            break;
        }
        positionX += size.width;
    }
    if (isReady) 
        break;
    positionX = 0;
    positionY += size.height;
}
return [NSArray arrayWithArray:tempArray];
  }
@end
于 2012-05-20T03:19:29.987 回答