首页 文章 精选 留言 我的

精选列表

搜索[集合],共10003篇文章
优秀的个人博客,低调大师

iOS 史上最全的图片压缩方法集合

做上传图片功能,特别是类似于微信,QQ里面,公布9张图片, 少不了碰到一个问题,就是图片压缩问题,当然我也遇到了. 我研究了这个问题,发现网上普遍的方法是例如以下 //压缩图片质量 +(UIImage*)reduceImage:(UIImage*)imagepercent:(float)percent { NSData*imageData=UIImageJPEGRepresentation(image,percent); UIImage*newImage=[UIImageimageWithData:imageData]; returnnewImage; } //压缩图片尺寸 +(UIImage*)imageWithImageSimple:(UIImage*)imagescaledToSize:(CGSize)newSize { //Createagraphicsimagecontext UIGraphicsBeginImageContext(newSize); //newsize [imagedrawInRect:CGRectMake(0,0,newSize.width,newSize.height)]; //Getthenewimagefromthecontext UIImage*newImage=UIGraphicsGetImageFromCurrentImageContext(); //Endthecontext UIGraphicsEndImageContext(); //Returnthenewimage. returnnewImage; } 上面的方法比較常见,但是须要载入到内存中来处理图片,当图片数量多了的时候就会收到内存警告,程序崩溃, 我測试过上面方法,上面方法真不好用,真的不推荐大家用.那么我推荐以下这种方法: staticsize_tgetAssetBytesCallback(voidvoid*info,voidvoid*buffer,off_tposition,size_tcount){ ALAssetRepresentation*rep=(__bridgeid)info; NSError*error=nil; size_tcountRead=[repgetBytes:(uint8_t*)bufferfromOffset:positionlength:counterror:&error]; if(countRead==0&&error){ //Wehavenowayofpassingthisinfobacktothecaller,sowelogit,atleast. NDDebug(@"thumbnailForAsset:maxPixelSize:gotanerrorreadinganasset:%@",error); } returncountRead; } staticvoidreleaseAssetCallback(voidvoid*info){ //TheinfohereisanALAssetRepresentationwhichweCFRetaininthumbnailForAsset:maxPixelSize:. //Thisreleasebalancesthatretain. CFRelease(info); } //ReturnsaUIImageforthegivenasset,withsizelengthatmostthepassedsize. //TheresultingUIImagewillbealreadyrotatedtoUIImageOrientationUp,soitsCGImageRef //canbeuseddirectlywithoutadditionalrotationhandling. //Thisisdonesynchronously,soyoushouldcallthismethodonabackgroundqueue/thread. -(UIImage*)thumbnailForAsset:(ALAsset*)assetmaxPixelSize:(NSUInteger)size{ NSParameterAssert(asset!=nil); NSParameterAssert(size>0); ALAssetRepresentation*rep=[assetdefaultRepresentation]; CGDataProviderDirectCallbackscallbacks={ .version=0, .getBytePointer=NULL, .releaseBytePointer=NULL, .getBytesAtPosition=getAssetBytesCallback, .releaseInfo=releaseAssetCallback, }; CGDataProviderRefprovider=CGDataProviderCreateDirect((voidvoid*)CFBridgingRetain(rep),[repsize],&callbacks); CGImageSourceRefsource=CGImageSourceCreateWithDataProvider(provider,NULL); CGImageRefimageRef=CGImageSourceCreateThumbnailAtIndex(source,0,(__bridgeCFDictionaryRef)@{ (NSString*)kCGImageSourceCreateThumbnailFromImageAlways:@YES, (NSString*)kCGImageSourceThumbnailMaxPixelSize:[NSNumbernumberWithInt:size], (NSString*)kCGImageSourceCreateThumbnailWithTransform:@YES, }); CFRelease(source); CFRelease(provider); if(!imageRef){ returnnil; } UIImage*toReturn=[UIImageimageWithCGImage:imageRef]; CFRelease(imageRef); returntoReturn; } 採用上面的方法之后内存占用率非常低。 上面这么长的东西,大家一定看的非常费劲,那么我直接上我源代码,给大家看看,照着我的源代码,写就好了. staticsize_tgetAssetBytesCallback(void*info,void*buffer,off_tposition,size_tcount) { ALAssetRepresentation*rep = (__bridgeid)info; NSError*error =nil; size_tcountRead = [repgetBytes:(uint8_t*)bufferfromOffset:positionlength:counterror:&error]; if(countRead ==0&& error) { // We have no way of passing this info back to the caller, so we log it, at least. NSLog(@"thumbnailForAsset:maxPixelSize: got an error reading an asset: %@", error); } returncountRead; } staticvoidreleaseAssetCallback(void*info) { // The info here is an ALAssetRepresentation which we CFRetain in thumbnailForAsset:maxPixelSize:. // This release balances that retain. CFRelease(info); } //压缩图片 - (UIImage*)thumbnailForAsset:(ALAsset*)asset maxPixelSize:(NSUInteger)size { NSParameterAssert(asset !=nil); NSParameterAssert(size >0); ALAssetRepresentation*rep = [assetdefaultRepresentation]; CGDataProviderDirectCallbackscallbacks = { .version=0, .getBytePointer=NULL, .releaseBytePointer=NULL, .getBytesAtPosition=getAssetBytesCallback, .releaseInfo=releaseAssetCallback, }; CGDataProviderRefprovider =CGDataProviderCreateDirect((void*)CFBridgingRetain(rep), [repsize], &callbacks); CGImageSourceRefsource =CGImageSourceCreateWithDataProvider(provider,NULL); CGImageRefimageRef =CGImageSourceCreateThumbnailAtIndex(source,0, (__bridgeCFDictionaryRef) @{ (NSString*)kCGImageSourceCreateThumbnailFromImageAlways:@YES, (NSString*)kCGImageSourceThumbnailMaxPixelSize: [NSNumbernumberWithInt:size], (NSString*)kCGImageSourceCreateThumbnailWithTransform:@YES, }); CFRelease(source); CFRelease(provider); if(!imageRef) { returnnil; } UIImage*toReturn = [UIImageimageWithCGImage:imageRef]; CFRelease(imageRef); returntoReturn; } 把上面的代码拷贝到project里面,然后调用这种方法就好了,例如以下图: 好了,有问题,欢迎评论给我..... 本文部分内容參考自http://www.lyitnews.com/portal.php?mod=view&aid=1206,感谢他的分享.... 本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/5114382.html,如需转载请自行联系原作者

优秀的个人博客,低调大师

OmniTools —— 自托管的强大网页工具集合

OmniTools,这是一款自托管的 Web 应用,提供各种在线工具来简化日常任务。无论是编写代码、处理图片/视频、PDF 还是处理数字,OmniTools 都能满足你的需求。这里是演示网站。 无广告,无追踪,只需通过浏览器即可快速访问实用工具。 所有文件均在客户端完全处理:所有内容都不会离开你的设备。此外,Docker 镜像非常轻量,仅 28MB,可快速部署并轻松实现自托管。

资源下载

更多资源
Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Spring

Spring

Spring框架(Spring Framework)是由Rod Johnson于2002年提出的开源Java企业级应用框架,旨在通过使用JavaBean替代传统EJB实现方式降低企业级编程开发的复杂性。该框架基于简单性、可测试性和松耦合性设计理念,提供核心容器、应用上下文、数据访问集成等模块,支持整合Hibernate、Struts等第三方框架,其适用范围不仅限于服务器端开发,绝大多数Java应用均可从中受益。

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

用户登录
用户注册
修改密码
忘记密码