PDRPluginResult 返回值 的最大长度问题,插件压缩图片返回Base64字符串,但图片像素width大于450的时候,就返回不了值了。
if (command) {
NSString cid=[command.arguments objectAtIndex:0];
NSString url=[command.arguments objectAtIndex:1];
NSString pysb=[command.arguments objectAtIndex:2];
NSString lsWidth=[command.arguments objectAtIndex:3];
NSString *lsHeight=[command.arguments objectAtIndex:4];
NSURL *aPath3=[NSURL URLWithString:url];
UIImage *img =[[UIImage alloc]initWithData:[NSData dataWithContentsOfURL:aPath3]];
float pysb2= [ pysb floatValue];
//新图片的长宽
int width=[lsWidth intValue];
float newwidth=width * pysb2;
if(newwidth>430){
newwidth=430;
}
NSData *imageData = nil;
NSString *mimeType = nil;
NSString *dataString=nil;
//压缩
UIImage *newImg=[self imageCompressForWidth:img targetWidth:newwidth];
if (UIImagePNGRepresentation(newImg) == nil) {
imageData = UIImageJPEGRepresentation(newImg, pysb2);
mimeType = @"image/jpeg";
} else {
imageData = UIImagePNGRepresentation(newImg);
mimeType = @"image/png";
}
dataString=[NSString stringWithFormat:@"%@",[imageData base64EncodedStringWithOptions: 0]];
PDRPluginResult *result = [PDRPluginResult resultWithStatus:PDRCommandStatusOK messageAsString:dataString];
[self toCallback:cid withReslut:[result toJSONString]];
}
//压缩方法
-(UIImage ) imageCompressForWidth:(UIImage )sourceImage targetWidth:(CGFloat)defineWidth{
UIImage newImage = nil;
CGSize imageSize = sourceImage.size;
CGFloat width = imageSize.width;
CGFloat height = imageSize.height;
CGFloat targetWidth = defineWidth;
CGFloat targetHeight = height / (width / targetWidth);
CGSize size = CGSizeMake(targetWidth, targetHeight);
CGFloat scaleFactor = 0.0;
CGFloat scaledWidth = targetWidth;
CGFloat scaledHeight = targetHeight;
CGPoint thumbnailPoint = CGPointMake(0.0, 0.0);
if(CGSizeEqualToSize(imageSize, size) == NO){
CGFloat widthFactor = targetWidth / width;
CGFloat heightFactor = targetHeight / height;
if(widthFactor > heightFactor){
scaleFactor = widthFactor;
}
else{
scaleFactor = heightFactor;
}
scaledWidth = width scaleFactor;
scaledHeight = height scaleFactor;
if(widthFactor > heightFactor){
thumbnailPoint.y = (targetHeight - scaledHeight) 0.5;
}else if(widthFactor < heightFactor){
thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
}
}
UIGraphicsBeginImageContext(size);
CGRect thumbnailRect = CGRectZero;
thumbnailRect.origin = thumbnailPoint;
thumbnailRect.size.width = scaledWidth;
thumbnailRect.size.height = scaledHeight;
[sourceImage drawInRect:thumbnailRect];
newImage = UIGraphicsGetImageFromCurrentImageContext();
if(newImage == nil){
NSLog(@"scale image fail");
}
UIGraphicsEndImageContext();
return newImage;
}
0 个回复