ios 图片拼接 图片缩放 将webView上面的内容转化为图片 归纳整理
2015-11-19 16:39
399 查看
//将image1拼接到image2上
- (UIImage *)addImage:(UIImage *)image1 toImage:(UIImage *)image2
{
@autoreleasepool {
CGSize size;
if (image1.size.width > image2.size.width)
{
size = CGSizeMake(image1.size.width, image1.size.height+image2.size.height);
} else {
size = CGSizeMake(image2.size.width, image1.size.height+image2.size.height);
}
UIGraphicsBeginImageContext(size);
// Draw image1
[image1 drawInRect:CGRectMake(image1.size.width > image2.size.width?0:(image2.size.width-image1.size.width)/2,
image2.size.height, image1.size.width, image1.size.height)];
// Draw image2
[image2 drawInRect:CGRectMake(image1.size.width > image2.size.width?(image1.size.width-image2.size.width)/2:0, 0,
image2.size.width, image2.size.height)];
UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return resultingImage;
}
}
//图片缩放
- (UIImage *)convertImage:(UIImage *)origImage scope:(CGFloat)scope
{
UIImage *image = nil;
CGSize size = origImage.size;
if (size.width <= scope && size.height <= scope) {
// do nothing
image = origImage;
} else {
CGFloat length = size.width;
if (size.width < size.height) {
length = size.width;
}
CGFloat f = scope/length;
CGSize newSize = CGSizeMake(size.width*f, size.height*f);
//
UIGraphicsBeginImageContext(newSize);
// Tell the old image to draw in this new context, with the desired
// new size
[origImage drawInRect:CGRectMake(0, 0, newSize.width,
newSize.height)];
// Get the new image from the context
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
return image;
}
//将webView上面的内容转化为图片
- (UIImage *)webContentImage{
CGSize boundsSize = web.bounds.size;
CGFloat boundsWidth = web.bounds.size.width;
CGFloat boundsHeight = web.bounds.size.height;
CGPoint offset = web.scrollView.contentOffset;
[web.scrollView setContentOffset:CGPointMake(0, 0)];
CGFloat contentHeight = web.scrollView.contentSize.height;
NSMutableArray *images = [NSMutableArray array];
while (contentHeight > 0) {
UIGraphicsBeginImageContext(boundsSize);
[web.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[images addObject:image];
CGFloat offsetY = web.scrollView.contentOffset.y;
[web.scrollView setContentOffset:CGPointMake(0, offsetY + boundsHeight)];
contentHeight -= boundsHeight;
}
[web.scrollView setContentOffset:offset];
UIGraphicsBeginImageContext(web.scrollView.contentSize);
[images enumerateObjectsUsingBlock:^(UIImage *image, NSUInteger idx, BOOL *stop) {
[image drawInRect:CGRectMake(0, boundsHeight * idx, boundsWidth, boundsHeight)];
}];
UIImage *fullImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return fullImage;
}
- (UIImage *)addImage:(UIImage *)image1 toImage:(UIImage *)image2
{
@autoreleasepool {
CGSize size;
if (image1.size.width > image2.size.width)
{
size = CGSizeMake(image1.size.width, image1.size.height+image2.size.height);
} else {
size = CGSizeMake(image2.size.width, image1.size.height+image2.size.height);
}
UIGraphicsBeginImageContext(size);
// Draw image1
[image1 drawInRect:CGRectMake(image1.size.width > image2.size.width?0:(image2.size.width-image1.size.width)/2,
image2.size.height, image1.size.width, image1.size.height)];
// Draw image2
[image2 drawInRect:CGRectMake(image1.size.width > image2.size.width?(image1.size.width-image2.size.width)/2:0, 0,
image2.size.width, image2.size.height)];
UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return resultingImage;
}
}
//图片缩放
- (UIImage *)convertImage:(UIImage *)origImage scope:(CGFloat)scope
{
UIImage *image = nil;
CGSize size = origImage.size;
if (size.width <= scope && size.height <= scope) {
// do nothing
image = origImage;
} else {
CGFloat length = size.width;
if (size.width < size.height) {
length = size.width;
}
CGFloat f = scope/length;
CGSize newSize = CGSizeMake(size.width*f, size.height*f);
//
UIGraphicsBeginImageContext(newSize);
// Tell the old image to draw in this new context, with the desired
// new size
[origImage drawInRect:CGRectMake(0, 0, newSize.width,
newSize.height)];
// Get the new image from the context
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
return image;
}
//将webView上面的内容转化为图片
- (UIImage *)webContentImage{
CGSize boundsSize = web.bounds.size;
CGFloat boundsWidth = web.bounds.size.width;
CGFloat boundsHeight = web.bounds.size.height;
CGPoint offset = web.scrollView.contentOffset;
[web.scrollView setContentOffset:CGPointMake(0, 0)];
CGFloat contentHeight = web.scrollView.contentSize.height;
NSMutableArray *images = [NSMutableArray array];
while (contentHeight > 0) {
UIGraphicsBeginImageContext(boundsSize);
[web.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[images addObject:image];
CGFloat offsetY = web.scrollView.contentOffset.y;
[web.scrollView setContentOffset:CGPointMake(0, offsetY + boundsHeight)];
contentHeight -= boundsHeight;
}
[web.scrollView setContentOffset:offset];
UIGraphicsBeginImageContext(web.scrollView.contentSize);
[images enumerateObjectsUsingBlock:^(UIImage *image, NSUInteger idx, BOOL *stop) {
[image drawInRect:CGRectMake(0, boundsHeight * idx, boundsWidth, boundsHeight)];
}];
UIImage *fullImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return fullImage;
}
相关文章推荐
- iOS TextView在TableviewCell中自动增加高度
- WCF:Maximum number of items that can be serialized or deserialized in an object graph is '65536'.
- iOS最基本的post请求和Unicode转码
- Android图案密码,手势锁源码解析
- Android应用开发的最简单的思路
- Android U盘拔插提示音分析
- Android Fragment 真正的完全解析(上)
- 微信、qq时间格式模板
- Android开发,关于引起内存泄露的资源汇总
- Android.mk 小结
- 6个值得推荐的Android开源框架简介
- 【Android自定义Dialog】电视机开关效果的Dialog
- Android Fragment 真正的完全解析(下)
- android 常见分辨率(mdpi、hdpi 、xhdpi、xxhdpi )屏幕适配
- Android material design support library -- CollapsingToolbarLayout简介
- Android WebView 的内存泄露问题
- Android中实现静态的默认安装和卸载应用
- 用Go写了一个可以用来访问goolge类似Proxy的小程序
- unity 程序切进切出停止其他音乐
- 【Android】多个EditText设置其中一个不可编辑并隐藏键盘