0

我有一个应用程序,我可以在其中捕获它工作正常的图像,但我想减小我从网上搜索到的图像的大小我发现了一些代码在这里

这是我的代码在捕获图像时工作正常

NSString *type = [info objectForKey:UIImagePickerControllerMediaType];

UIImage *pickedImage = [info objectForKey:UIImagePickerControllerOriginalImage];

NSData *imageData = UIImagePNGRepresentation(pickedImage);

NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

我想减小拾取图像的大小我得到了以下代码,但我没有像使用 newSize 那样使用它,而是使用原始图像进行压缩

UIGraphicsBeginImageContext(newSize);
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();   
4

3 回答 3

0

你能做的是......

NSString *type = [info objectForKey:UIImagePickerControllerMediaType];

UIImage *pickedImage = [info objectForKey:UIImagePickerControllerOriginalImage];

 CGSize sizeCropped = CGSizeMake(602, 450);//you can adjust any size here...

 pickedImage = [yourclassname imageWithImage:pickedImage scaledToSize:sizeCropped];//call below function...




+ (UIImage*)imageWithImage:(UIImage*)image
          scaledToSize:(CGSize)newSize;
{
UIGraphicsBeginImageContext( newSize );
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return newImage;
 }

让我知道它是否有效!!!!

编码快乐!!!!

于 2013-07-22T06:35:13.903 回答
0
+(UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize;
{
    UIGraphicsBeginImageContext( newSize ); 
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

您可以使用这些代码行在需要压缩图像的地方调用此函数。

    CGSize firstSize = CGSizeMake(210.0,210.0);
    UIImage *compImage=[mainclass imageWithImage:image scaledToSize:firstSize];
于 2013-07-22T06:31:53.200 回答
0

如果您想以新大小保存该新图像,那么在您的代码之后将该图像添加到库中,并带有波纹管..

UIImageWriteToSavedPhotosAlbum(UIImage *image, id completionTarget, SEL completionSelector, void *contextInfo);

前任:

UIGraphicsBeginImageContext(newSize);
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(newImage, nil, nil, nil);
于 2013-07-22T06:45:27.643 回答