最近项目有一个需求,就是需要在应用内截一张图(超出屏幕的也要截到),然后把截出来的图分享出去,只分享图片。
首先我要解决第一个问题,就是在应用内截图,我在网上找到了对应的解决方法,代码如下
//根据一个View生成一个image
- (UIImage*)screenshotForView:(UIView*)view {
UIImage*image =nil;
//判断View类型(一般不是滚动视图或者其子类的话内容不会超过一屏,当然如果超过了也可以通过修改frame来实现绘制)
if ([view.class isSubclassOfClass:[UIScrollView class]]) {
UIScrollView*scrView = (UIScrollView*)view;
CGPointtempContentOffset = scrView.contentOffset;
CGRecttempFrame = scrView.frame;
scrView.contentOffset = CGPointZero;
scrView.frame = CGRectMake(0, 0, scrView.contentSize.width, scrView.contentSize.height);
image = [selfscreenshotForView:scrViewsize:scrView.frame.size];
scrView.contentOffset= tempContentOffset;
scrView.frame= tempFrame;
}else{
image = [selfscreenshotForView:viewsize:view.frame.size];
}
returnimage;
}
- (UIImage*)screenshotForView:(UIView*)view size:(CGSize)size {
UIImage*image =nil;
UIGraphicsBeginImageContextWithOptions(size, YES, [UIScreen mainScreen].scale);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
image =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
returnimage;
}
调screenshotForView这个方法就可以获取到截来的图,我用的是MOB分享,分享类型SSDKContentTypeImage,只传图片,内容和标题都不传,分享到微信后,发现图片有6M多,需要压缩,然后通过下面的代码实现了压缩,压缩到了900多kb,但是分辨率也下降了,基本满足了需求
- (NSData*)resetSizeOfImageData:(UIImage*)sourceImage maxSize:(NSInteger)maxSize {
//先判断当前质量是否满足要求,不满足再进行压缩
__blockNSData*finallImageData =UIImageJPEGRepresentation(sourceImage,1.0);
NSUIntegersizeOrigin = finallImageData.length;
NSUIntegersizeOriginKB = sizeOrigin /1000;
if(sizeOriginKB <= maxSize) {
returnfinallImageData;
}
//获取原图片宽高比
CGFloatsourceImageAspectRatio = sourceImage.size.width/ sourceImage.size.height;
//先调整分辨率
CGSizedefaultSize =CGSizeMake(1024,1024/ sourceImageAspectRatio);
UIImage* newImage = [selfnewSizeImage:defaultSizeimage:sourceImage];
finallImageData =UIImageJPEGRepresentation(newImage,1.0);
//保存压缩系数
NSMutableArray*compressionQualityArr = [NSMutableArrayarray];
CGFloatavg =1.0/250;
CGFloatvalue = avg;
for(inti =250; i >=1; i --) {
value = i * avg;
[compressionQualityArraddObject:@(value)];
}
/*
调整大小
说明:压缩系数数组compressionQualityArr是从大到小存储。
*/
//思路:使用二分法搜索
finallImageData = [selfhalfFuntion:compressionQualityArrimage:newImagesourceData:finallImageDatamaxSize:maxSize];
//如果还是未能压缩到指定大小,则进行降分辨率
while(finallImageData.length==0) {
//每次降100分辨率
CGFloatreduceWidth =100.0;
CGFloatreduceHeight =100.0/ sourceImageAspectRatio;
if(defaultSize.width- reduceWidth <=0|| defaultSize.height- reduceHeight <=0) {
break;
}
defaultSize =CGSizeMake(defaultSize.width- reduceWidth, defaultSize.height- reduceHeight);
UIImage*image = [selfnewSizeImage:defaultSize
image:[UIImageimageWithData:UIImageJPEGRepresentation(newImage, [[compressionQualityArrlastObject]floatValue])]];
finallImageData = [selfhalfFuntion:compressionQualityArrimage:imagesourceData:UIImageJPEGRepresentation(image,1.0)maxSize:maxSize];
}
returnfinallImageData;
}
#pragma mark 调整图片分辨率/尺寸(等比例缩放)
- (UIImage*)newSizeImage:(CGSize)size image:(UIImage*)sourceImage {
CGSizenewSize =CGSizeMake(sourceImage.size.width, sourceImage.size.height);
CGFloattempHeight = newSize.height/ size.height;
CGFloattempWidth = newSize.width/ size.width;
if(tempWidth >1.0&& tempWidth > tempHeight) {
newSize =CGSizeMake(sourceImage.size.width/ tempWidth, sourceImage.size.height/ tempWidth);
}elseif(tempHeight >1.0&& tempWidth < tempHeight) {
newSize =CGSizeMake(sourceImage.size.width/ tempHeight, sourceImage.size.height/ tempHeight);
}
UIGraphicsBeginImageContext(newSize);
[sourceImagedrawInRect:CGRectMake(0,0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
returnnewImage;
}
#pragma mark 二分法
- (NSData*)halfFuntion:(NSArray*)arr image:(UIImage*)image sourceData:(NSData*)finallImageData maxSize:(NSInteger)maxSize {
NSData*tempData = [NSDatadata];
NSUIntegerstart =0;
NSUIntegerend = arr.count-1;
NSUIntegerindex =0;
NSUIntegerdifference =NSIntegerMax;
while(start <= end) {
index = start + (end - start) /2;
finallImageData =UIImageJPEGRepresentation(image, [arr[index]floatValue]);
NSUIntegersizeOrigin = finallImageData.length;
NSUIntegersizeOriginKB = sizeOrigin /1024;
NSLog(@"当前降到的质量:%ld", (unsignedlong)sizeOriginKB);
NSLog(@"\nstart:%zd\nend:%zd\nindex:%zd\n压缩系数:%lf", start, end, (unsignedlong)index, [arr[index] floatValue]);
if(sizeOriginKB > maxSize) {
start = index +1;
}elseif(sizeOriginKB < maxSize) {
if(maxSize - sizeOriginKB < difference) {
difference = maxSize - sizeOriginKB;
tempData = finallImageData;
}
if(index <=0) {
break;
}
end = index -1;
}else{
break;
}
}
returntempData;
}
这样我就实现了截图,压缩和分享