日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

UIImageView圆角,自适应图片宽高比例,图片拉伸,缩放比例和图片缩微图

發(fā)布時間:2023/11/29 编程问答 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 UIImageView圆角,自适应图片宽高比例,图片拉伸,缩放比例和图片缩微图 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

?/*
???? 設(shè)置圓角,通過layer中的cornerRadius和masksToBounds即可。
??? ?
???? 自適應(yīng)圖片寬高比例。通過UIViewContentModeScaleAspectFit設(shè)置,注意這個UIImageView的frame就不是init中的數(shù)據(jù)了。
??? ?
???? 同樣的UIImage圖片放入不同frame中的UIImageView就可以實現(xiàn)比例縮放了。只是UIImageView的大小改變了,

??? ?
???? */
??? UIImage* image = [UIImage imageNamed:@"back2.jpg"];? ?
??? UIImageView* imageView1 = [[[UIImageView alloc] initWithImage:image] autorelease];? ?
??? imageView1.frame = CGRectMake(0, 0, 300, 200);? ?
??? imageView1.center = CGPointMake(150, 200);
??? //設(shè)置圓角
??? imageView1.layer.cornerRadius = 8; ?
??? imageView1.layer.masksToBounds = YES;
?? ?
??? //自適應(yīng)圖片寬高比例
??? imageView1.contentMode = UIViewContentModeScaleAspectFit; ?
??? [self.view addSubview:imageView1]; ?
?? ?
?? ?
??? //拉伸圖片
??? CGFloat capWidth = image.size.width / 2; ?
??? CGFloat capHeight = image.size.height / 2; ?
??? UIImage* stretchableImage = [image stretchableImageWithLeftCapWidth:capWidth topCapHeight:capHeight];
??? UIImageView* imageView3 = [[[UIImageView alloc] initWithImage:stretchableImage] autorelease];
??? imageView3.frame = CGRectMake(0, 0, 300, 200);? ?
??? imageView3.center = CGPointMake(150, 200); ?
??? [self.view addSubview:imageView3];
?? ?
??? //改變frame改變
??? UIImageView* imageView4 = [[[UIImageView alloc] initWithImage:image] autorelease];
??? imageView4.frame = CGRectMake(0, 0, 300/2, 200/2);? ?
??? imageView4.center = CGPointMake(150, 200); ?

??? [self.view addSubview:imageView4];

?

//縮微圖

- (UIImage *)generatePhotoThumbnail:(UIImage *)image {

? ? // Create a thumbnail version of the image for the event object.

? ? CGSize size = image.size;

? ? CGSize croppedSize;

? ? CGFloat ratioX = 75.0; ??

? ? CGFloat ratioY = 60.0;

? ? CGFloat offsetX = 0.0;

? ? CGFloat offsetY = 0.0;

?? ?

? ? // check the size of the image, we want to make it

? ? // a square with sides the size of the smallest dimension

? ? if (size.width > size.height) {

? ? ? ? offsetX = (size.height - size.width) / 2;

? ? ? ? croppedSize = CGSizeMake(size.height, size.height);

? ? } else {

? ? ? ? offsetY = (size.width - size.height) / 2;

? ? ? ? croppedSize = CGSizeMake(size.width, size.width);

? ? }

?? ?

? ? // Crop the image before resize

? ? CGRect clippedRect = CGRectMake(offsetX * -1, offsetY * -1, croppedSize.width, croppedSize.height);

? ? CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], clippedRect);

? ? // Done cropping

? ? // Resize the image

? ? CGRect rect = CGRectMake(0.0, 0.0, ratioX, ratioY); // 設(shè)置圖片縮微圖的區(qū)域((0,0),寬:75 ?高:60)

? ? UIGraphicsBeginImageContext(rect.size);

? ? [[UIImage imageWithCGImage:imageRef] drawInRect:rect];

? ? UIImage *thumbnail = UIGraphicsGetImageFromCurrentImageContext();

? ? UIGraphicsEndImageContext();

? ? // Done Resizing

? ? return thumbnail;

}

總結(jié)

以上是生活随笔為你收集整理的UIImageView圆角,自适应图片宽高比例,图片拉伸,缩放比例和图片缩微图的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。