当前位置:   article > 正文

UIGraphicsBeginImageContext UIGraphicsGetImageFromCurrentImageContext 压缩,修改,截取图片_uigraphicsgetimagefromcurrentimagecontext() nil

uigraphicsgetimagefromcurrentimagecontext() nil
  1. //获取图片的缩略图
  2. + (UIImage *)thumbnailWithImageWithoutScale:(UIImage *)image size:(CGSize)asize{
  3. UIImage *newimage;
  4. if (nil == image) {
  5. newimage = nil;
  6. }
  7. else{
  8. CGSize oldsize = image.size;
  9. CGRect rect;
  10. if (asize.width/asize.height > oldsize.width/oldsize.height) {
  11. rect.size.width = asize.height*oldsize.width/oldsize.height;
  12. rect.size.height = asize.height;
  13. rect.origin.x = (asize.width - rect.size.width)/2;
  14. rect.origin.y = 0;
  15. }
  16. else{
  17. rect.size.width = asize.width;
  18. rect.size.height = asize.width*oldsize.height/oldsize.width;
  19. rect.origin.x = 0;
  20. rect.origin.y = (asize.height - rect.size.height)/2;
  21. }
  22. UIGraphicsBeginImageContext(asize);
  23. CGContextRef context = UIGraphicsGetCurrentContext();
  24. CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]);
  25. UIRectFill(CGRectMake(0, 0, asize.width, asize.height));//clear background
  26. [image drawInRect:rect];
  27. newimage = UIGraphicsGetImageFromCurrentImageContext();
  28. UIGraphicsEndImageContext();
  29. }
  30. return newimage;
  31. }
  32. //获取等比例压缩的图像
  33. + (UIImage *)getPicZoomImage:(UIImage *)image {
  34. UIImage *img = image;
  35. int h = img.size.height;
  36. int w = img.size.width;
  37. if(h <= PicAfterZoomWidth && w <= PicAfterZoomHeight)
  38. {
  39. image = img;
  40. }
  41. else
  42. {
  43. float b = (float)PicAfterZoomWidth/w < (float)PicAfterZoomHeight/h ? (float)PicAfterZoomWidth/w : (float)PicAfterZoomHeight/h;
  44. CGSize itemSize = CGSizeMake(b*w, b*h);
  45. UIGraphicsBeginImageContext(itemSize);
  46. CGRect imageRect = CGRectMake(0, 0, b*w, b*h);
  47. [img drawInRect:imageRect];
  48. img = UIGraphicsGetImageFromCurrentImageContext();
  49. UIGraphicsEndImageContext();
  50. }
  51. return img;
  52. }
  53. /**
  54. * 修改图片大小
  55. */
  56. + (UIImage *) imageWithImageSimple:(UIImage*)image scaledToSize:(CGSize) newSize{
  57. newSize.height=image.size.height*(newSize.width/image.size.width);
  58. UIGraphicsBeginImageContext(newSize);
  59. [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
  60. UIImage *newImage=UIGraphicsGetImageFromCurrentImageContext();
  61. UIGraphicsEndImageContext();
  62. return newImage;
  63. }
  64. //截取图片 根据frame
  65. - (NSArray *)captureViews:(UIView *)theView frame:(NSArray *)fra{
  66. @autoreleasepool {
  67. NSMutableArray *imageViewArrReturn = [NSMutableArray array];
  68. UIGraphicsBeginImageContext(theView.frame.size);
  69. CGContextRef context = UIGraphicsGetCurrentContext();
  70. [theView.layer renderInContext:context];
  71. theView.layer.contents =nil;
  72. UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
  73. UIGraphicsEndImageContext();
  74. for (int i = 0; i < fra.count; i++) {
  75. NSValue *frame = [fra objectAtIndex:i];
  76. CGImageRef ref = CGImageCreateWithImageInRect(img.CGImage, [frame CGRectValue]);
  77. UIImage *image = [UIImage imageWithCGImage:ref];
  78. [imageViewArrReturn addObject:image];
  79. CGImageRelease(ref);
  80. }
  81. CGContextRelease(context);
  82. return imageViewArrReturn;
  83. }
  84. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/484661
推荐阅读
相关标签
  

闽ICP备14008679号