转向:@H_403_3@
我对比率元素进行了相当多的修改,以便能够正确地检测要修剪的数量:使用图像的较短边,构建基于short_sIDe / wIDth_of_desired_rect的比率,但在这种情况下似乎不起作用.帮助赞赏!@H_403_3@
- (UIImage *)squareImageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize { double ratio; double delta; CGPoint offset; //make a new square size,that is the resized imaged wIDth CGSize sz = CGSizeMake(newSize.wIDth,newSize.wIDth); //figure out if the picture is landscape or portrait,then //calculate scale factor and offset if (image.size.wIDth > image.size.height) { ratio = newSize.wIDth / image.size.wIDth; delta = (ratio*image.size.wIDth - ratio*image.size.height); offset = CGPointMake(delta/2,0); } else { ratio = newSize.wIDth / image.size.height; delta = (ratio*image.size.height - ratio*image.size.wIDth); offset = CGPointMake(0,delta/2); } //make the final clipPing rect based on the calculated values CGRect clipRect = CGRectMake(-offset.x,-offset.y,(ratio * image.size.wIDth) + delta,(ratio * image.size.height) + delta); //for retina consIDeration if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) { UIGraphicsBeginImageContextWithOptions(sz,YES,0.0); } else { UIGraphicsBeginImageContext(sz); } UIRectClip(clipRect); [image drawInRect:clipRect]; UIImage *newImage = UIGraphicsGetimageFromCurrentimageContext(); UIGraphicsEndImageContext(); return newImage;}解决方法 试试这个:
- (UIImage *)squareImageFromImage:(UIImage *)image scaledToSize:(CGfloat)newSize { CGAffinetransform scaletransform; CGPoint origin; if (image.size.wIDth > image.size.height) { CGfloat scaleRatio = newSize / image.size.height; scaletransform = CGAffinetransformMakeScale(scaleRatio,scaleRatio); origin = CGPointMake(-(image.size.wIDth - image.size.height) / 2.0f,0); } else { CGfloat scaleRatio = newSize / image.size.wIDth; scaletransform = CGAffinetransformMakeScale(scaleRatio,scaleRatio); origin = CGPointMake(0,-(image.size.height - image.size.wIDth) / 2.0f); } CGSize size = CGSizeMake(newSize,newSize); if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) { UIGraphicsBeginImageContextWithOptions(size,0); } else { UIGraphicsBeginImageContext(size); } CGContextRef context = UIGraphicsGetCurrentContext(); CGContextConcatCTM(context,scaletransform); [image drawAtPoint:origin]; image = UIGraphicsGetimageFromCurrentimageContext(); UIGraphicsEndImageContext(); return image;}总结
以上是内存溢出为你收集整理的ios – 制作UIImage的正方形裁剪图像以拉伸图像全部内容,希望文章能够帮你解决ios – 制作UIImage的正方形裁剪图像以拉伸图像所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)