문제

I have app in which i capture the image it works fine but i want to reduce the size of the image i searched from net i found some code which is here

This is my code working fine when image captured

NSString *type = [info objectForKey:UIImagePickerControllerMediaType];

UIImage *pickedImage = [info objectForKey:UIImagePickerControllerOriginalImage];

NSData *imageData = UIImagePNGRepresentation(pickedImage);

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

I want to reduce the size of picked image i got the following code but i am did not getting it use like it use newSize but where it is using the original image to compress

UIGraphicsBeginImageContext(newSize);
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();   
도움이 되었습니까?

해결책

what you can do is....

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;
 }

let me know it is working or not!!!!

Happy coding!!!!!

다른 팁

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

you can call this function where you want the compressed image using these lines of code.

    CGSize firstSize = CGSizeMake(210.0,210.0);
    UIImage *compImage=[mainclass imageWithImage:image scaledToSize:firstSize];

if you want to save that new Image with new size then after your code add that image in library with bellow line..

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

EX:

UIGraphicsBeginImageContext(newSize);
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(newImage, nil, nil, nil);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top