In system photo library, open my action extension, because image is too big, extension crashed, how can I get a smaller image?
I try to use this following code, but it not work.
NSDictionary * dict =@{NSItemProviderPreferredImageSizeKey:[NSValue valueWithCGSize:CGSizeMake(300, 300)]};
[itemProvider loadItemForTypeIdentifier:(NSString *)kUTTypeImage options:dict completionHandler:^(UIImage *image, NSError *error) {
}];
Before load the image on imageView. First resize it. Then load the resized image on imageView.
UIImage *img = [UIImage imageWithData:selectedImgdata];
selectedImage = [self resizeImage:img];
Resize image Data:
-(UIImage *)resizeImage :(UIImage *)theImage
{
if((theImage.size.width > 1024 && theImage.size.height > 768) || (theImage.size.width > 768 && theImage.size.height > 1024) || (theImage.size.width > 1024 && theImage.size.height < 768) || (theImage.size.height > 1024 && theImage.size.width < 768))
{
float newHeight,newWidth,oldWidth,oldHeight,scaleFactor;
if(theImage.size.width > theImage.size.height)
{
oldWidth = theImage.size.width;
scaleFactor = 1024 / oldWidth;
newHeight = theImage.size.height * scaleFactor;
newWidth = oldWidth * scaleFactor;
}
else
{
oldHeight = theImage.size.height;
scaleFactor = 1024 / oldHeight;
newWidth = theImage.size.width * scaleFactor;
newHeight = oldHeight * scaleFactor;
}
UIGraphicsBeginImageContextWithOptions(CGSizeMake(newWidth, newHeight), NO, 1.0);
[theImage drawInRect:CGRectMake(0, 0, newWidth, newHeight)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
return theImage;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.