在无线端开发中,
有时候会遇到展示一张超大图片,不允许压缩,如果传给后台。会考虑到对图片的裁剪。
对于内存的考虑。
写了一个demo,用来裁剪图片,或者展示超大图片
无线端展示:
- (void)viewDidLoad {
[super viewDidLoad];
[self prepareWebView];
}
- (void)prepareWebView{
WKWebView * webView = [[WKWebView alloc] initWithFrame:[UIScreen mainScreen].bounds];
UIImage *image = [UIImage imageNamed:@"IMG_0270.jpg"];
NSString *imageSting = [self htmlForJPGImage:image];
NSString *content = [NSString stringWithFormat:@"<html><body>%@</body></html>", imageSting];
[webView loadHTMLString:content baseURL:nil];
webView.navigationDelegate = self;
[self.view addSubview:webView];
}
- (NSString *)htmlForJPGImage:(UIImage *)image{
NSData *imageData = UIImageJPEGRepresentation(image,1.f);
NSString *imageSource = [NSString stringWithFormat:@"data:image/jpg;base64,%@",[imageData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength]];
return [NSString stringWithFormat:@"<div align=center><img src='%@' /></div>", imageSource];
}
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation
{
}
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
{
decisionHandler(WKNavigationActionPolicyAllow);
}
图片裁剪:将图片分割成固定大小的小图片标识出坐标x y 以及宽高。
- (void)setBigImage:(UIImage *)image
{
_bigImage = image;
self.bigImagewidth = _bigImage.size.width;
self.bigImagehight = _bigImage.size.height;
NSInteger j = 0;
NSMutableArray *preceImages = [NSMutableArray array];
while (self.bigImagewidth > (j + 1) * PieceHeight) {
[preceImages addObjectsFromArray:[self cutImageWithOfY:j]];
j += 1;
}
if (self.bigImagewidth > j * PieceHeight && self.bigImagewidth < (j + 1) * PieceHeight) {
[preceImages addObjectsFromArray:[self cutImageWithOfY:j]];
}
self.pieces = preceImages;
}
- (NSArray *)cutImageWithOfY:(NSInteger )j
{
NSInteger i = 0;
NSMutableArray *preceImages = [NSMutableArray array];
while (self.bigImagewidth > (i + 1) * PieceWidth ) {
ImagePieceModel *pieceModel = [[ImagePieceModel alloc] init];
pieceModel.width = PieceWidth;
pieceModel.height = PieceHeight;
pieceModel.imageX = i * PieceWidth;
pieceModel.imageY = j * PieceHeight;
pieceModel.image = [self cutBigImage:_bigImage wide_X:pieceModel.imageX heigh_Y:pieceModel.imageY wideCount:PieceWidth heightCount:PieceHeight];
[preceImages addObject:pieceModel];
i += 1;
}
if (self.bigImagewidth > i * PieceWidth && self.bigImagewidth < (i + 1) * PieceWidth) {
ImagePieceModel *pieceModel = [[ImagePieceModel alloc] init];
pieceModel.width = self.bigImagewidth - i * PieceWidth;
pieceModel.height = PieceHeight;
pieceModel.imageX = i * PieceWidth;
pieceModel.imageY = j * PieceHeight;
pieceModel.image = [self cutBigImage:_bigImage wide_X:pieceModel.imageX heigh_Y:pieceModel.imageY wideCount:pieceModel.width heightCount:pieceModel.height];
[preceImages addObject:pieceModel];
}
return preceImages;
}
- (UIImage *)cutBigImage:(UIImage *)bigImage wide_X:(CGFloat)X heigh_Y:(CGFloat)Y wideCount:(CGFloat)wideCount heightCount:(CGFloat)heightCount
{
CGRect smallRect = CGRectMake(X, Y, wideCount, heightCount);
CGImageRef smallImageRef = CGImageCreateWithImageInRect(bigImage.CGImage, smallRect);
UIImage* smallImage = [UIImage imageWithCGImage: smallImageRef];
return smallImage;
}