我的应用程序有麻烦了,我期待你的帮助。我想上传特定大小的照片**(1200*1800)**从库到服务器,我需要获得原始图像,然后压缩它。
UIImage *image = [UIImage imageWithCGImage:[asset fullResolutionImage] scale:[asset scale] orientation:0];不幸的是,如果我的应用程序的原始图像大小大于20M,就会崩溃。那么,有什么方法可以直接从AssetsLibrary获得具有特定大小的图像吗?
发布于 2013-07-18 08:31:28
我只是放了一些有用的代码,我不确定,但在您的情况下可能会有帮助:
用于获取作物图像的:
UIImage *croppedImg = nil;
CGRect cropRect = CGRectMake(AS YOu Need);
croppedImg = [self croppIngimageByImageName:self.imageView.image toRect:cropRect];使用以下返回UIImage的方法(如您所需的图像大小)
- (UIImage *)croppIngimageByImageName:(UIImage *)imageToCrop toRect:(CGRect)rect
{
//CGRect CropRect = CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height+15);
CGImageRef imageRef = CGImageCreateWithImageInRect([imageToCrop CGImage], rect);
UIImage *cropped = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return cropped;
}在这里,您得到的作物图像,返回上述方法;
或调整大小
并采用以下方法对特定的高宽和宽度与图像进行大小调整UIImage
+ (UIImage*)resizeImage:(UIImage*)image withWidth:(int)width withHeight:(int)height
{
CGSize newSize = CGSizeMake(width, height);
float widthRatio = newSize.width/image.size.width;
float heightRatio = newSize.height/image.size.height;
if(widthRatio > heightRatio)
{
newSize=CGSizeMake(image.size.width*heightRatio,image.size.height*heightRatio);
}
else
{
newSize=CGSizeMake(image.size.width*widthRatio,image.size.height*widthRatio);
}
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}此方法返回具有所需特定大小的NewImage。
https://stackoverflow.com/questions/17718185
复制相似问题