此时我突发奇想,UILabel可以自适应高度,而且可以展示Html,那么一定也可以展示多张图片的.但是多图片预览又要点击效果,网上搜索了很久发现都是提示要改成UITextView,但是UITextView又不能自动适应高度(只能使用KVO),所以就有了此贴.
我们可以将图片的url添加为image标签,点击时只需比对点击区域和图片frame即可,如图所示,点击后会d窗提示点击第几张图片
体验请点击demo的github地址: 这里 ,好用记得给颗星星哦!
这种绘制是根据图片的像素比例 等比例进行绘制的,在选择图片和创建展示图片的imageView 时,注意查看尺寸 注:绘图时使用 [UIScreen mainScreen].scale 可以是图片更清晰 UIGraphicsBeginImageContextWithOptions(image.size, NO, [UIScreen mainScreen].scale)//这样就不模糊了
//图片上添加文字 详细版
- (UIImage*)text:(NSString*)text addToView:(UIImage*)image{
//设置字体样式
UIFont*font = [UIFont fontWithName:@"Arial-BoldItalicMT"size:100]
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:text]
[str addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0, 1)]
[str addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:100] range:NSMakeRange(0, text.length)]
// CGSize textSize = [text sizeWithAttributes:dict]
CGSize textSize = [str size]
//绘制上下文
UIGraphicsBeginImageContext(image.size)
//UIGraphicsBeginImageContextWithOptions(image.size, NO, [UIScreen mainScreen].scale)//这样就不模糊了
[image drawInRect:CGRectMake(0,0, image.size.width, image.size.height)]
// int border =10
CGRect re = {CGPointMake((image.size.width- textSize.width)/2, 200), textSize}
// CGRect rect = CGRectMake(0, 0, image.size.width, 500)
//此方法必须写在上下文才生效
[str drawInRect:re ]
UIImage*newImage =UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage
}
//修改图片尺寸
- (UIImage*)imageWithImageSimple:(UIImage*)image scaledToSize:(CGSize)newSize
{
// Create a graphics image context
UIGraphicsBeginImageContext(newSize)//这样压缩的图片展示出来会很模糊
//UIGraphicsBeginImageContextWithOptions(image.size, NO, [UIScreen mainScreen].scale)//这样就不模糊了
//UIGraphicsBeginImageContextWithOptions(image.size, NO, [UIScreen mainScreen].scale)//这样就不模糊了
// Tell the old image to draw in this new context, with the desired
// new size
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)]
// Get the new image from the context
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext()
// End the context
UIGraphicsEndImageContext()
// Return the new image.
return newImage
}
//圆角
- (UIImage *) getRadioImaeg:(NSString *)imageName1{
UIImage *image1 = [UIImage imageNamed:imageName1]
UIGraphicsBeginImageContextWithOptions(image1.size, 0, 0)
CGContextRef ctx = UIGraphicsGetCurrentContext()
CGRect rect = CGRectMake(00, 0, image1.size.width, image1.size.width)
CGContextAddEllipseInRect(ctx, rect)
CGContextClip(ctx)
[image1 drawInRect:rect]
UIImage *img = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return img
}
//图片叠加
- (UIImage *)addImage:(NSString *)imageName1 withImage:(NSString *)imageName2 {
UIImage *image1 = [UIImage imageNamed:imageName1]
UIImage *image2 = [self getRadioImaeg:@"333"]
UIGraphicsBeginImageContext(image1.size)
//UIGraphicsBeginImageContextWithOptions(image.size, NO, [UIScreen mainScreen].scale)//这样就不模糊了
[image1 drawInRect:CGRectMake(0, 0, image1.size.width, image1.size.height)]
[image2 drawInRect:CGRectMake((image1.size.width - image2.size.width)/2,(image1.size.height - image2.size.height)/2, image2.size.width, image2.size.height)]
UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return resultingImage
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)