如何得到uiimage 大小的大小

主题 : UIImageView在设置contentMode之后,如何获得image的大小?
级别: 新手上路
可可豆: 11 CB
威望: 11 点
在线时间: 5(时)
发自: Web Page
来源于&&分类
UIImageView在设置contentMode之后,如何获得image的大小?&&&
如果把contentMode设置为UIViewContentModeScaleAspectFit,如何获得图片的大小(不是原始大小)?
级别: 精灵王
UID: 124167
发帖: 1923
可可豆: 2674 CB
威望: 5053 点
在线时间: 1113(时)
发自: Web Page
设都设了,还获取大小干啥?
级别: 新手上路
可可豆: 11 CB
威望: 11 点
在线时间: 5(时)
发自: Web Page
回 1楼(raykle) 的帖子
需要在图片的右上角添加一个删除的按钮,这个按钮的frame不好确定。。。
级别: 精灵王
UID: 124167
发帖: 1923
可可豆: 2674 CB
威望: 5053 点
在线时间: 1113(时)
发自: Web Page
回 2楼(cold_418) 的帖子
哦,那只能自己计算了。计算原始图片按照按钮的大小缩放之后变成多大,然后再设置删除按钮的frame。
级别: 侠客
UID: 86392
可可豆: 564 CB
威望: 560 点
在线时间: 183(时)
发自: Web Page
设置之前的image的size能拿到,然后根据image view的rect计算等比缩放的比例,就能的出来了
关注本帖(如果有新回复会站内信通知您)
9*6-8 正确答案:46
发帖、回帖都会得到可观的积分奖励。
按"Ctrl+Enter"直接提交
关注CocoaChina
关注微信 每日推荐
扫一扫 浏览移动版下次自动登录
现在的位置:
& 综合 & 正文
ios开发-适配图片大小
有的时候,从服务器或者本地加载进去的一系列图片的尺寸可能不能,这在我们显示图片的时候可能会遇到一些问题。
比如把UIImageView的frame写死。
比如固定width,固定height。这样,可能有的图片会正好显示,但有的图片就会出现扭曲变形了。
这时候,显然应该根据每个图片的实际尺寸设置UIImageView的frame。
下面说说实现方法,很简单。
一。声明一个UIImage加载图片
UIImage *pic=[UIImage
imageNamed:_img];
其中,_img为图片名称。
如"myPic.png"
二。获得图片尺寸
pic.size.height
pic.size.width
得到这2个float类型的数据。
三。把尺寸应用到UIImageView中。
比如我的处理方法,固定宽度width,让height进行适配。
UIImageView *imageview = [[UIImageView
initWithFrame:CGRectMake(20,
280, 280*pic.size.height/pic.size.width)];
这里我把宽度固定为280.
学习的路上,与君共勉。
&&&&推荐文章:
【上篇】【下篇】当前访客身份:游客 [
懂得太少,表现太多;才华太少,锋芒太多,浅薄.
目前还没有任何评论
今日访问:18
昨日访问:34
本周访问:101
本月访问:294
所有访问:3151
iOS--OC--图片拉伸和尺寸变换,图片简单处理
发表于11个月前( 21:01)&&
阅读(2414)&|&评论()
0人收藏此文章,
iOS--OC--图片拉伸和尺寸变换
图片拉伸和尺寸变换
图片拉伸 :
-&(UIImage&*)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth&topCapHeight:(NSInteger)topCapH
一般 leftCapWidth = imageWidth *.5f, topCapHeight = imageWidth * .5f;
尺寸变换:
//resize图片
-&(UIImage&*)reSizeImage:(UIImage&*)image&toSize:(CGSize)reSize{
&&&&UIGraphicsBeginImageContext(CGSizeMake(reSize.width,&reSize.height));
&&&&[image&drawInRect:CGRectMake(0,&0,&reSize.width,&reSize.height)];
&&&&UIImage&*reSizeImage&=&UIGraphicsGetImageFromCurrentImageContext();
&&&&UIGraphicsEndImageContext();
&&&&return&reSizeI
图片的处理大概分 截图(capture), &缩放(scale), 设定大小(resize), &存储(save)
1.等比率缩放
-&(UIImage&*)scaleImage:(UIImage&*)image&toScale:(float)scaleSize{
&&&&UIGraphicsBeginImageContext(CGSizeMake(image.size.width&*&scaleSize,&image.size.height&*&scaleSize);
&&&&[image&drawInRect:CGRectMake(0,&0,&image.size.width&*&scaleSize,&image.size.height&*&scaleSize)];
&&&&UIImage&*scaledImage&=&UIGraphicsGetImageFromCurrentImageContext();
&&&&UIGraphicsEndImageContext();
&&&&return&scaledI
2.自定长宽
-&(UIImage&*)reSizeImage:(UIImage&*)image&toSize:(CGSize)reSize{
&&&&UIGraphicsBeginImageContext(CGSizeMake(reSize.width,&reSize.height));
&&&&[image&drawInRect:CGRectMake(0,&0,&reSize.width,&reSize.height)];
&&&&UIImage&*reSizeImage&=&UIGraphicsGetImageFromCurrentImageContext();
&&&&UIGraphicsEndImageContext();
&&&&return&reSizeI
3.处理某个特定View只要是继承UIView的object 都可以处理必须先import QuzrtzCore.framework
-(UIImage*)captureView:(UIView&*)theView
&&&&CGRect&rect&=&theView.UIGraphicsBeginImageContext(rect.size);
&&&&CGContextRef&context&=&UIGraphicsGetCurrentContext();
&&&&[theView.layer&renderInContext:context];
&&&&UIImage&*img&=&UIGraphicsGetImageFromCurrentImageContext();
&&&&UIGraphicsEndImageContext();
&&&&return&
4.储存图片储存图片这里分成储存到app的文件里和储存到手机的图片库里
//1)&储存到app的文件里
&&&&NSString&*path&=&[[NSHomeDirectory()stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:@"image.png"];
&&&&[UIImagePNGRepresentation(image)&writeToFile:pathatomically:YES];
//把要处理的图片,&以image.png名称存到app&home下的Documents目录里
//2)储存到手机的图片库里(必须在真机使用,模拟器无法使用)
&&&&CGImageRef&screen&=&UIGetScreenImage();UIImage*&image&=&[UIImage&imageWithCGImage:screen];
&&&&CGImageRelease(screen);
&&&&UIImageWriteToSavedPhotosAlbum(image,&self,&nil,&nil);
&&&&UIGetScreenImage();&//&原来是private(私有)api,&用来截取整个画面,不过SDK&4.0后apple就开放了
//====================================================================================
以下代码用到了Quartz Framework 和 Core Graphics Framework. 在workspace的framework目录里添加这两个framework.在UIKit里,图像类UIImage和CGImageRef的画图操作都是通过Graphics Context来完成。Graphics Context封装了变换的参数,使得在不同的坐标系里操作图像非常方便。缺点就是,获取图像的数据不是那么方便。下面会给出获取数据区的代码。
1. 从UIView中获取图像相当于窗口截屏。
(ios提供全局的全屏截屏函数UIGetScreenView(). 如果需要特定区域的图像,可以crop一下)
CGImageRef&screen&=&UIGetScreenImage();
UIImage*&image&=&[UIImage&imageWithCGImage:screen];
2. 对于特定UIView的截屏。
(可以把当前View的layer,输出到一个ImageContext中,然后利用这个ImageContext得到UIImage)
-(UIImage*)captureView:&(UIView&*)theView
&&&&CGRect&rect&=&theView.
&&&&UIGraphicsBeginImageContext(rect.size);
&&&&CGContextRef&context&=UIGraphicsGetCurrentContext();
&&&&[theView.layer&renderInContext:context];
&&&&UIImage&*img&=&UIGraphicsGetImageFromCurrentImageContext();
&&&&UIGraphicsEndImageContext();
&&&&return&
3. 如果需要裁剪指定区域。
(可以path & clip,以下例子是建一个200x200的图像上下文,再截取出左上角)
UIGraphicsBeginImageContext(CGMakeSize(200,200));
CGContextRefcontext=UIGraphicsGetCurrentContext();
UIGraphicsPushContext(context);
//&...把图写到context中,省略[indent]CGContextBeginPath();
CGContextAddRect(CGMakeRect(0,0,100,100));
CGContextClosePath();[/indent]CGContextDrawPath();
CGContextFlush();&//&强制执行上面定义的操作
UIImage*&image&=&UIGraphicGetImageFromCurrentImageContext();
UIGraphicsPopContext();
4. 存储图像。
(分别存储到home目录文件和图片库文件。)
存储到目录文件是这样
NSString&*path&=&[[NSHomeDirectory()&stringByAppendingPathComponent:@"Documents"]&stringByAppendingPathComponent:@"image.png"];
[UIImagePNGRepresentation(image)&writeToFile:path&atomically:YES];
若要存储到图片库里面
UIImageWriteToSavedPhotosAlbum(image,&nil,&nil,&nil);
5.&&互相转换UImage和CGImage。
//(UImage封装了CGImage,&互相转换很容易)
UIImage*&imUI=
CGImageRef&imCG=
imUI&=&[UIImage&initWithCGImage:imCG];
imCG&=&imUI.CGI
6. 从CGImage上获取图像数据区。
(在apple dev上有QA, 不过好像还不支持ios)
下面给出一个在ios上反色的例子
-(id)invertContrast:(UIImage*)img{
CGImageRef&inImage&=&img.CGI&
CGContextRef&
CFDataRef&m_DataR
m_DataRef&=&CGDataProviderCopyData(CGImageGetDataProvider(inImage));&
int&width&=&CGImageGetWidth(&inImage&);
int&height&=&CGImageGetHeight(&inImage&);
int&bpc&=&CGImageGetBitsPerComponent(inImage);
int&bpp&=&CGImageGetBitsPerPixel(inImage);
int&bpl&=&CGImageGetBytesPerRow(inImage);
UInt8&*&m_PixelBuf&=&(UInt8&*)&CFDataGetBytePtr(m_DataRef);
int&length&=&CFDataGetLength(m_DataRef);
NSLog(@"len&%d",&length);
NSLog(@"width=%d,&height=%d",&width,&height);
NSLog(@"1=%d,&2=%d,&3=%d",&bpc,&bpp,bpl);
for&(int&index&=&0;&index&&&&index&+=&4)
m_PixelBuf[index&+&0]&=&255&-&m_PixelBuf[index&+&0];//&b
m_PixelBuf[index&+&1]&=&255&-&m_PixelBuf[index&+&1];//&g
m_PixelBuf[index&+&2]&=&255&-&m_PixelBuf[index&+&2];//&r
ctx&=&CGBitmapContextCreate(m_PixelBuf,&width,&height,&bpb,&bpl,&CGImageGetColorSpace(&inImage&),&kCGImageAlphaPremultipliedFirst&);
CGImageRef&imageRef&=&CGBitmapContextCreateImage&(ctx);
UIImage*&rawImage&=&[UIImage&imageWithCGImage:imageRef];
CGContextRelease(ctx);
return&rawI
7.&显示图像数据区。
(显示图像数据区,也就是unsigned&char*转为graphics&context或者UIImage或和CGImageRef)
CGContextRef&ctx&=&CGBitmapContextCreate(pixelBuf,width,height,&bitsPerComponent,bypesPerLine,&colorSpace,kCGImageAlphaPremultipliedLast&);
CGImageRef&imageRef&=&CGBitmapContextCreateImage&(ctx);
UIImage*&image&=&[UIImage&imageWithCGImage:imageRef];
NSString*&path&=&[[NSHomeDirectory()&stringByAppendingPathComponent:@"Documents"]&stringByAppendingPathComponent:@"ss.png"];
[UIImagePNGRepresentation(self.image)&writeToFile:path&atomically:YES];
CGContextRelease(ctx);
更多开发者职位上
1)">1)">1" ng-class="{current:{{currentPage==page}}}" ng-repeat="page in pages"><li class='page' ng-if="(endIndex<li class='page next' ng-if="(currentPage
相关文章阅读如何得到UIImage的大小_百度知道
如何得到UIImage的大小
我有更好的答案
width * height * bitsPerPixel / 8bitsPerPixel可以用CGImage的 CGImageGetBitsPerPixel 函数获取,大部分情况下BPP默认是 32 Bit
其他类似问题
为您推荐:
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁二次元同好交流新大陆
扫码下载App
汇聚2000万达人的兴趣社区下载即送20张免费照片冲印
扫码下载App
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!&&|&&
LOFTER精选
网易考拉推荐
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
阅读(2506)|
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
历史上的今天
loftPermalink:'',
id:'fks_',
blogTitle:'UIImage 图片处理:截图,缩放,设定大小,存储',
blogAbstract:'图片的处理大概就分 截图(capture), 缩放(scale),设定大小(resize), 存储(save)这几样比较好处理, 另外还有滤镜,擦试等, 以后再说在这个Demo code裡, 我写了几个方法 1.等比率缩放- (UIImage *)scaleImage:(UIImage *)image toScale:(float)scaleSize { UIGraphicsBeginImageContext(CGSizeMake(image.size.width * scaleSize, image.size.height * scaleSize);',
blogTag:'',
blogUrl:'blog/static/',
isPublished:1,
istop:false,
modifyTime:5,
publishTime:0,
permalink:'blog/static/',
commentCount:0,
mainCommentCount:0,
recommendCount:0,
bsrk:-100,
publisherId:0,
recomBlogHome:false,
currentRecomBlog:false,
attachmentsFileIds:[],
groupInfo:{},
friendstatus:'none',
followstatus:'unFollow',
pubSucc:'',
visitorProvince:'',
visitorCity:'',
visitorNewUser:false,
postAddInfo:{},
mset:'000',
remindgoodnightblog:false,
isBlackVisitor:false,
isShowYodaoAd:false,
hostIntro:'',
hmcon:'0',
selfRecomBlogCount:'0',
lofter_single:''
{list a as x}
{if x.moveFrom=='wap'}
{elseif x.moveFrom=='iphone'}
{elseif x.moveFrom=='android'}
{elseif x.moveFrom=='mobile'}
${a.selfIntro|escape}{if great260}${suplement}{/if}
{list a as x}
推荐过这篇日志的人:
{list a as x}
{if !!b&&b.length>0}
他们还推荐了:
{list b as y}
转载记录:
{list d as x}
{list a as x}
{list a as x}
{list a as x}
{list a as x}
{if x_index>4}{break}{/if}
${fn2(x.publishTime,'yyyy-MM-dd HH:mm:ss')}
{list a as x}
{if !!(blogDetail.preBlogPermalink)}
{if !!(blogDetail.nextBlogPermalink)}
{list a as x}
{if defined('newslist')&&newslist.length>0}
{list newslist as x}
{if x_index>7}{break}{/if}
{list a as x}
{var first_option =}
{list x.voteDetailList as voteToOption}
{if voteToOption==1}
{if first_option==false},{/if}&&“${b[voteToOption_index]}”&&
{if (x.role!="-1") },“我是${c[x.role]}”&&{/if}
&&&&&&&&${fn1(x.voteTime)}
{if x.userName==''}{/if}
网易公司版权所有&&
{list x.l as y}
{if defined('wl')}
{list wl as x}{/list}}

我要回帖

更多关于 设置uiimage大小 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信