![](/img/trans.png)
[英]RootViewController - tableView:cellForRowAtIndexPath - Load image from assetsLibrary
[英]iOS : How to get image with specific size from AssetsLibrary
我的應用程序遇到了麻煩,希望得到您的幫助。 我想將特定尺寸的照片**(1200 * 1800)**從庫上傳到服務器,我需要獲取原始圖像然后進行壓縮。
UIImage *image = [UIImage imageWithCGImage:[asset fullResolutionImage] scale:[asset scale] orientation:0];
不幸的是,如果原始圖像大小大於20M,我的應用程序將崩潰。 那么,有什么方法可以直接從AssetsLibrary獲取具有特定大小的圖像?
我只是輸入一些有用的代碼,我不確定,但是可能對您有用:
對於獲取作物圖像:
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 ,具有所需的特定大小。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.