我有什么方法可以让用户将图像上传到应用程序中,例如50X150像素的图像,我可以将其分成3个50x50像素的图像吗?
如果是这样,有人可以帮助我选择某些像素并将其分成几个图像吗?
谢谢!
答案 0 :(得分:2)
使用此代码...
//在以下方法 inRect:(CGRect)rect >>>这个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;
}
For more visit this reference..
希望,这会帮助你......享受
答案 1 :(得分: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)];
答案 2 :(得分: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