我的问题是:我从服务器获取图像。当我从服务器图像大小138x91获取图像但我给我的图像大小是我的自定义单元格大小85x56。它装载好。但是,当我单击行时,它会增加服务器图像大小。如何在我的tableview中获取图像时减小图像大小以及当我单击row时如何不改变图像大小。解决我的问题。我希望图像从url获取以及如何在iOS中减小图像大小.goto bellow link。
1)。点击之前。
http://radiokhushi.com/siri-admin/uploads/Beforclick.png
2)。。点击
http://radiokhushi.com/siri-admin/uploads/ofterclick.png
这是图像获取索引路径中行的单元格中的数据代码。在显示一个placeholderimage之前加载。
newsobject.imageView.image = [UIImage imageNamed:@"Placeholder.png"];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"%@",[[newsarry objectAtIndex:indexPath.row] objectForKey:@"thumb_file"]]];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:data];
dispatch_async(dispatch_get_main_queue(), ^{
[tableView cellForRowAtIndexPath:indexPath].imageView.image = image;
});
});
答案 0 :(得分:7)
试试这个方法:
UIImage *croppedimg = [self imageWithImage:[UIImage imageNamed:@"image.png"] convertToSize:CGSizeMake(95, 95)]; // here resize your image whatever you want.
- (UIImage *)imageWithImage:(UIImage *)image convertToSize:(CGSize)size
{
UIGraphicsBeginImageContext(size);
[image drawInRect:CGRectMake(0, 0, size.width, size.height)];
UIImage *destImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return destImage;
}
答案 1 :(得分:0)
您可以调整图片大小。获得合适的图像大小。
-(UIImage *)resizeImage:(UIImage *)image withWidth:(int) width withHeight:(int) height {
CGImageRef imageRef = [image CGImage];
CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(imageRef);
CGColorSpaceRef colorSpaceInfo = CGColorSpaceCreateDeviceRGB();
if (alphaInfo == kCGImageAlphaNone)
alphaInfo = kCGImageAlphaNoneSkipLast;
CGContextRef bitmap;
if (image.imageOrientation == UIImageOrientationUp | image.imageOrientation == UIImageOrientationDown) {
bitmap = CGBitmapContextCreate(NULL, width, height, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, alphaInfo);
} else {
bitmap = CGBitmapContextCreate(NULL, height, width, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, alphaInfo);
}
if (image.imageOrientation == UIImageOrientationLeft) {
NSLog(@"image orientation left");
CGContextRotateCTM (bitmap, DEGREES_TO_RADIANS(90));
CGContextTranslateCTM (bitmap, 0, -height);
} else if (image.imageOrientation == UIImageOrientationRight) {
NSLog(@"image orientation right");
CGContextRotateCTM (bitmap, DEGREES_TO_RADIANS(-90));
CGContextTranslateCTM (bitmap, -width, 0);
} else if (image.imageOrientation == UIImageOrientationUp) {
NSLog(@"image orientation up");
} else if (image.imageOrientation == UIImageOrientationDown) {
NSLog(@"image orientation down");
CGContextTranslateCTM (bitmap, width,height);
CGContextRotateCTM (bitmap, DEGREES_TO_RADIANS(-180.));
}
CGContextDrawImage(bitmap, CGRectMake(0, 0, width, height), imageRef);
CGImageRef ref = CGBitmapContextCreateImage(bitmap);
UIImage *result = [UIImage imageWithCGImage:ref];
CGContextRelease(bitmap);
CGImageRelease(ref);
return result;
}
答案 2 :(得分:0)
如果没有在tableview委托中看到代码,很难确定究竟是什么问题。
但是,我在想如果你设置一个合适的contentMode
imageView将解决你的问题。
设置图像后,相应地设置contentMode,可能是这样的:
newsobject.imageView.image = [UIImage imageNamed:@"Placeholder.png"];
newsobject.imageView.contentMode = UIViewContentModeScaleToFill;
答案 3 :(得分:0)
使用UIImageView+WebCache.h & .m
类下载图片。您可以在github或google上搜索这些类。它为您提供了异步下载图像的不同方法,并提供了根据我们的应用程序要求调整图像大小的功能。使用以下方法:
- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options: (SDWebImageOptions)options andResize:(CGSize)size withContentMode:(UIViewContentMode)mode;
我希望这可以帮助您解决问题。
答案 4 :(得分:0)
[newsobject.allnewsimg setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[[newsarry objectAtIndex:indexPath.row] objectForKey:@"image_file"]]]
placeholderImage:[UIImage imageNamed:@"songs85-56.png"]];